What is causing this ActiveRecord::ReadOnlyRecord error ?
The object :readonly => true to finder method will mark returned records as read-only. The :joins option now implies :readonly, And saving the same record will now fail. Use find_by_sql to work.
By using find_by_sql it returns raw row,column data, not a ActiveRecords.they have some method:
- Force the instance variable @readonly to false in the record.
- :include => :card Alternative :join => :card
Rails 2.3.4 and 3.0.0:
- Record.find_by_sql is a viable option.
- :readonly => true is automatically inferred only if :joins was specified without an explicit :select nor an explicit :readonly option the implementation of set_readonly_option! in active_record/base.rb for Rails 2.3.4, or the implementation of to_a in active_record/relation.rb and of custom_join_sql in active_record/relation/query_methods.rb for Rails 3.0.0
- :readonly => true is always automatically inferred in has_and_belongs_to_many if the join table has more than the two foreign keys columns and :joins was specified without an explicit :select user-supplied :readonly values are ignored — see finding_with_ambiguous_select? in active_record/associations/has_and_belongs_to_many_association.rb.
- conclusion, unless dealing with a special join table and has_and_belongs_to_many, then @aaronrustad‘s answer applies just fine in Rails 2.3.4 and 3.0.0.
- Do not use :includes if you want to achieve an INNER JOIN :includes implies a LEFT OUTER JOIN, which is less selective and less efficient than INNER JOIN