首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > Ruby Rails >

代码片断-readonlyrecord error and paginate joins where

2012-09-08 
代码片段-readonlyrecord error and paginate joins wherefuns短文?@funs Fun.where( taggings.tag_id

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

热点排行