代码片段-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?

This might have changed in recent release of Rails, but the appropriate way to solve this problem is to add  :readonly => false  to the find options.

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:

  1. Force the instance variable @readonly to false in the record (hack)
  2. Use :include => :card instead of :join => :card

def join_tags(post)
     post.tags.map { |t| t.name }.join(", ")
   end

你可能感兴趣的:(activerecord,taggings)