开发者

How i can set instance variable using mass assignment in Rails

开发者 https://www.devze.com 2023-03-18 19:22 出处:网络
What I have: class User < ActiveRecord::Base attr_accessor :notify end User.new(args) What I\'m doing:

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

精彩评论

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