代码片断-readonlyrecord error and paginate joins where
funs短文
@funs = Fun.where( "taggings.tag_id = #{params[:tag_id].to_i}").joins(" right join taggings on funs.id = taggable_id").paginate(:page => params[:page], :per_page => 10, :include => :user, :readonly => false).order ( 'funs.created_at DESC')
What is causing this ActiveRecord::ReadOnlyRecord error?
Introduce read-only records. If you call object.readonly! then it will mark the object as read-only and raise ReadOnlyRecord if you call object.save. object.readonly? reports whether the object is read-only. Passing :readonly => true to any finder method will mark returned records as read-only. The :joins option now implies :readonly, so if you use this option, saving the same record will now fail. Use find_by_sql to work around.
Using find_by_sql
is not really an alternative as it returns raw row/column data, notActiveRecords
. You have two options:
- Force the instance variable
@readonly
to false in the record (hack) - Use
:include => :card
instead of:join => :card
def join_tags(post)
post.tags.map { |t| t.name }.join(", ")
end