Good Rails3 ActiveRecord Finder vs Very Good Rails4 ActiveRecord Finder

Well, previouse article I had talked about Keyword Arguments Feature in Ruby 2.0. now we can apply keyword arguments in Rails4.

FINDERS

old-style finders are deprecated
- Rails3:

Rails3
1
Post.find(:all, conditions: { author: 'admin'})

Warning: Calling #find(:all) is deprecated. Please call #all directly instead. You have also used finder options. Please build s scope instead of using finder options.

- Rails4:

Rails4
1
Post.where(author: 'admin')

Dynamic finder that return collections are deprecated
- Rails3:

Rails3
1
2
Post.find_all_by_title('Rails 4')
Post.find_last_by_author('admin')

Warning: This dynamic method is deprecated. Please use e.g Post.where(…).all instead.

- Rails4:

Rails4
1
2
Post.where(title: 'Rails 4')
Post.where(author: 'admin').last

FIND_BY

- Rails3:

Rails3
1
2
Post.find_by_title('Rails 4') # Dynamic find_by finders that take a single argument are not deprecated.
Post.find_by_title('Rails 4', conditions: { author: 'admin' }) # Dynamic find_by finders with conditions are deprecated.

- Rails4:

Rails4
1
2
Post.find_by(title: 'Rails 4')
Post.find_by(title: 'Rails 4', author: 'admin')

FIND_BY WITH HASH

allows dynamic input more easily
- Rails4:

Rails4
1
2
3
4
post_params = { title: 'Rails 4', author: 'admin' }
Post.find_by(post_params)

Post.find_by("published_on < ?", 2.weeks.ago)

FIND_OR_*

dynamic finders that create new objects are deprecated
- Rails3:

Rails3
1
2
Post.find_or_initialize_by_title('Rails 4')
Post.find_or_create_by_title('Rails 4')

Warning: This dinamic method is deprecated. Please use e.g Post.find_or_initialize_by(name: ‘foo’) instead.
Warning: This dinamic method is deprecated. Please use e.g Post.find_or_create_by(name: ‘foo’) instead.

- Rails4:

Rails4
1
2
Post.find_or_initialize_by(title: 'Rails 4')
Post.find_or_create_by(title: 'Rails 4')

So far so good, let upgrade to Rails4 then refactor ActiveRecord Finder together. :)