Ruby: Don't a add return in ensure
This method won't throw an error:
def a_method
raise
ensure
return :something
end
it will in fact return :something
So please proceed with care :)
Related cards:
Database: Scopes, migrations, and indices
Wether you modify an existing named scope or add a new one, or when you write a new query, make sure you have the proper indices.
This particularly applies if you're going to run non-trivial queries of course (admin backends, analytics, etc).
##...
Rebase your feature branches
Regularly, but at least before merging your feature branches, rebase them to get rid of "fix typo" and "wip" commits.
Getting rid of unnecessary commits
Let's say you do a git rebase -i HEAD~~~~
and have this...
Method return value should always be of same type
One of the main source of bugs and complexity in the code is when a functional method (that we expect to return a value) return different values under different circumstances.
For example, we ruby programmers have a bad habit of returning nil fro...
Creating a gem in lib folder
Go to lib folder and use bundler to generate main files for a gem:
$ bundle gem test_gem
create test_gem/Gemfile
create test_gem/Rakefile
create test_gem/LICENSE
create test_gem/README.md
...
When you set date attributes, you should not pass times
As you know, time zones make stuff a bit more difficult but are necessary.
A time-zoned record is converted to UTC using to_s(:db)
to be st...
Add indexes on foreign keys when you create a migration (with foreign key)
Whenever you make a migration to add a foreign key, you should also add an index for it
def self.up
add_column :comments, :user_id, :integer
add_index :comments, :user_id
end
DateTimes are Dates, beware
Given:
datetime = DateTime.now
date = datetime.to_date #or Date.today
will assert:
datetime.is_a? Date == true
datetime.is_a? DateTime == true
datetime.instance_of? Date == false
datetime.instance_of? DateTime == true
In case y...
ActiveRecord::NamedScopes (2.3.x) obtaining the SQL conditions
It's a good pratice to chain several named scopes like:
Property.listable.for_2_or_more_guests.best_10_properties
Now, to make the lesson more valuable let's assume the following code:
Property.scoped(:conditions => "foo = 2").scoped(...
Bundle install in parallel
Gave a shot to the new Bundler 1.4.0RC1 during the weekend and found out it now supports gem installation in parallel. To invoke you need to pass --jobs
parameter with number of threads you want it to run – for me the best performance was achiev...
Killing wkhtmltopdf during cucumber
wkhtmltopdf
hangs on mac during cucumber unless we click on it. The main reason is with the version we use which is 0.11.0_rc1 and in out app/bin we have another version and it is a known issue with these versions. The fix is to go to 0.9.9, to ...