Replaced by :on => :create...
From rails 3, before_validation_on_create has been removed and replaced with: before_validation :foo, :on => :create
View ArticleRemoved in 3.1.x (ActionView::Helpers::TextHelper#auto_link)
This method (and #auto_link_urls) has been removed in Rails 3.1 - other options are out there, such as Rinku, however there is a gem you can use for migration purposes etc, which is rails_autolink:...
View ArticleCatching and throwing -- don't! (ActiveRecord::Transactions::ClassMethods)
@wiseleyb and @glosakti, neither of your suggestions are necessary, and both are bad practices. This test: test "transactions" do assert_raises ZeroDivisionError do User.transaction do 1/0 end end end...
View ArticleLinks and basic explanation (ActiveRecord::QueryMethods#includes)
This function, available on any Base derived class, allows eager loading. So when iterating over a result, all the data can be pre-cached, reducing the number of queries from 1+x to 2. Several ways to...
View Articleflash messages (ActionController::Base#redirect_to)
In rails 3.1 the following does not work for me redirect_to { :action=>'atom' }, :alert => "Something serious happened" Instead, you need to use the following syntax (wrap with parens)...
View ArticleReorder (ActiveRecord::QueryMethods#order)
If you want to override previously set order (even through default_scope), use reorder() instead. E.g. User.order('id ASC').reorder('name DESC') would ignore ordering by id completely
View ArticleMakes it possible to use a scope through an association...
This is a very useful method if you want to to use a scope through an association: class Book < ActiveRecord::Base scope :available, where(:available => true) end class Author <...
View ArticleHTML5 data- attributes using RESTful approach...
HTML5 specifies extensible attributes like data-foo=“bar” (or as in Twitter Bootstrap data-toggle=“modal”), which poses two problems for Rails. First, if you’re using symbol notation in link_to to...
View ArticleMUCH easier way to pluralize without the number......
Just use Ruby! "string".pluralize(count)
View Articlerest of code is in NilClass#try (Object#try)
If you click “Show source” here, you may get confused. The logic for #try is shared between this method and NilClass#try . Both versions are currently implemented in the file...
View ArticleUsage (ActionDispatch::Routing::RouteSet#recognize_path)
In Rails 3.X console: Rails.application.routes.recognize_path('/my_path')
View ArticleBeware - May cause performance issues (ActiveRecord::Base#serialize)
A serialized attribute will always be updated during save, even if it was not changed. (A rails 3 commit explains why: http://github.com/rails/rails/issues/8328#issuecomment-10756812) Guard save calls...
View Articleuse validates :name, :presence => true instead...
validates_presence_of is a holdover from the Rails 2 days. This is the way it is done now http://guides.rubyonrails.org/active_record_validations_callbacks.html#presence
View ArticleUse strings as parameters, not booleans...
I just stumbled across this somewhere in our codebase. The first example is faulty, the second one is correct. = f.check_box :public, {}, true, false # <input id="event_public" name="event[public]"...
View ArticleIn Rails 4 it DOES return nil, even if the object you try from isn't nil...
The try method does not raise a NoMethodError if you call a method that doesn’t exist on an object. a = Article.new a.try(:author) #=> #<Author ...> nil.try(:doesnt_exist) #=> nil...
View ArticleFind the First Instance in the Table. If None Exists, Create One....
Specify the data you’re looking for. If it exists in the table, the first instance will be returned. If not, then create is called. If a block is provided, that block will be executed only if a new...
View ArticleOpposite of persisted? (ActiveRecord::Persistence#new_record?)
So I can find it when I look next time.
View ArticleText improvement (ActiveRecord::Calculations#pluck)
Where it says: without loading a bunch of records should say: without loading a bunch of columns/attributes Considering that record usually is a row.
View ArticleCreates record by given attributes only if table is empty...
This method first searches the table for ANY FIRST RECORD, not the one matching given attributes. If no record is found at all, it creates one using the specified attributes. This might be...
View ArticleRE: FALSE: Creates record by given attributes only if table is empty...
Dino’s comment was almost certainly due to a misunderstanding of the API, a mistake I made myself the first time I used it, and one I’ve witnessed multiple others make as well.The fact that this method...
View Article
More Pages to Explore .....