How to test for (ActiveRecord) object equality
Rails deliberately delegates equality checks to the identity column. If you want to know if two AR objects contain the same stuff, compare the result of calling #attributes on both.
Rails deliberately delegates equality checks to the identity column. If you want to know if two AR objects contain the same stuff, compare the result of calling #attributes on both.
There is a known issue about it on Github. According to this comment you might want to override the structurally_incompatible_values_for_or to overcome the issue: def structurally_incompatible_values_for_or(other) Relation::SINGLE_VALUE_METHODS.reject { |m| send(“#{m}_value”) == other.send(“#{m}_value”) } + (Relation::MULTI_VALUE_METHODS – [:eager_load, :references, :extending]).reject { |m| send(“#{m}_values”) == other.send(“#{m}_values”) } + (Relation::CLAUSE_METHODS – [:having, :where]).reject { |m| send(“#{m}_clause”) == other.send(“#{m}_clause”) … Read more
Rails will automatically generate the method smart? if there is a field named ‘smart’.
In Postgres you can do: @user.comments.group(“DATE_TRUNC(‘month’, created_at)”).count to get: {“2012-08-01 00:00:00″=>152, “2012-07-01 00:00:00″=>57, “2012-09-01 00:00:00″=>132} It accepts values from “microseconds” to “millennium” for grouping: http://www.postgresql.org/docs/8.1/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC
ActiveRecord .create method supports bulk creation. The method emulates the feature if the DB doesn’t support it and uses the underlying DB engine if the feature is supported. Just pass an array of options. # Create an Array of new objects User.create([{ :first_name => ‘Jamie’ }, { :first_name => ‘Jeremy’ }]) Block is supported and … Read more
The & symbol is used to denote that the following argument should be treated as the block given to the method. That means that if it’s not a Proc object yet, its to_proc method will be called to transform it into one. Thus, your example results in something like Post.all.map(&:id.to_proc) which in turn is equivalent … Read more
Of course use good old ruby’s attr_accessor. In your model: attr_accessor :foo, :bar You’ll be able to do: object.foo = ‘baz’ object.foo #=> ‘baz’
You need to add has_many :entries To each of your models, since the :through option just specifies a second association which it should use to find the other side.
Use reorder: Authors.books.reorder(‘name DESC’)
As per the following question, if you are using Rails >= 3.1, you can use object.dup : What is the easiest way to duplicate an activerecord record?