开发者

How i can set instance variable using mass assignment in Rails

What I have:

class User < ActiveRecord::Base
  attr_accessor :notify
 end

User.new(args)

What I'm doing:

开发者_运维知识库
User.new(args)
User.notify = true

What I need is something like:

User.new(args.merge(:notify => true))


What you have shown will work. Make sure to include :notify in the list of attributes for attr_accessible if you are using that.


As long as you have the attr_accessor defined, including the variable in any ActiveRecord mass assignment will work. As "Wizard of Ogz" mentioned, limiting access to the attributes affects both database attributes and instance variables alike.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜