Activerecord mass assignment to virtual attribute
I'm posting the following datas:
{"commit"=&开发者_StackOverflow社区gt;"Create", "conversation"=>{... , "watchers_ids"=>["2", "3", "4", "5", ...]}}
To the following action
def create
@conversation = @current_project.conversations.new(params[:conversation])
...
end
And the following class
class Conversation < RoleRecord
include Watchable
end
With this module
module Watchable
def self.included(model)
model.attr_accessible :watchers_ids
end
def watchers_ids=(ids)
add_watchers( ids )
end
def watchers_ids
...
end
...
end
However, the mass assignment don't work with the virtual attribute. Any idea?
There's code that's missing there...
If you have a has_and_belongs_to_many :watchers
you should be able to assign watcher_ids
without having to do this custom module stuff.
Then if you can't do that then that attribute is protected by some attr_protected
or, more likely, attr_accessible
call in your model. You can set it manually:
@conversation.watcher_ids = params[:conversation][:watcher_ids]
精彩评论