[英]Rails 4 upgrading - attr_accessor
I'm upgrading my application from rails 3.2 to rails 4.2.5. 我正在将我的应用程序从rails 3.2升级到rails 4.2.5。
In one of my model i have attr_accessor :user_id and i have defined a getter and setter methods, as i need to value on conditional basic. 在我的一个模型中,我有attr_accessor:user_id,我已经定义了一个getter和setter方法,因为我需要在条件基础上进行值。
class DummyModel < ActiveRecord::Base
attr_accessor :user_id
belongs_to :current_user
before_save :set_user_id
def user_id
self[:user_id] ? self[:user_id] : current_user_id ? current_user.id : nil
end
def user_id=(value)
self[:user_id] = value ? value : current_user_id ? current_user.id : nil
end
private
def set_user_id
self.current_user_id = CurrentUser.first_or_create(:user_id => self[:user_id]).id
end
end
In the above DummyModel, that table doesn't have user_id. 在上面的DummyModel中,该表没有user_id。 But a referance current_user_id which needs to be update by the params user_id which i get from browser.
但是需要通过我从浏览器获得的params user_id更新referance current_user_id。
So when i do DummyModel.new(params[:dummy_model]), it will try to assign the params[:dummy_model][:user_id] to the attr_accessor (With the setter method = user_id=(value)). 因此,当我执行DummyModel.new(params [:dummy_model])时,它将尝试将params [:dummy_model] [:user_id]分配给attr_accessor(使用setter方法= user_id =(value))。 Once assigned it can be used in the before_save.
一旦分配,它可以在before_save中使用。
This whole thing was working properly in rails 3.2. 整个过程在导轨3.2中正常工作。 But when i tried to update to rails 4.2, im getting this error message, as assigning value for attr_accessor with self[:user_id] syntax is removed.
但是,当我尝试更新到rails 4.2时,即时收到此错误消息,因为为self [:user_id]语法分配attr_accessor的值将被删除。
ActiveModel::MissingAttributeError: can't write unknown attribute `user_id`
So i have one solution to rename the user_id to something else, but user_id is used is many places of my application its not possible to change. 所以我有一个解决方案将user_id重命名为其他东西,但是user_id被用于我的应用程序的许多地方,它无法改变。
Hope this explains my problem. 希望这能解释我的问题。 Thanks for the help in advance.
我在这里先向您的帮助表示感谢。
I'm not sure why you're doing this but there's no point using attr_accessor if you're going to replace the methods it is generating. 我不确定你为什么要这样做但是如果要替换它正在生成的方法那么使用attr_accessor没有意义。 You may as well use the instance variable directly, for example
例如,您也可以直接使用实例变量
def user_id
@user_id || current_user_id
end
def user_id=(value)
@user_id = value || current_user_id
end
If you prefer attr_accessible, you could use it in Rails 4 too. 如果你更喜欢attr_accessible,你也可以在Rails 4中使用它。 You should install it like gem:
你应该像gem一样安装它:
gem 'protected_attributes'
What i can understand from the above but in rails 4 now there are strong parameters. 从上面我可以理解但是在rails 4中现在有很强的参数。 attr_accessor still can be initialized in rails 4 and that still gives you virtual attributes, but from above it looks like user_id is a physical attribute on the model level.
attr_accessor仍然可以在rails 4中初始化,并且仍然提供虚拟属性,但从上面看,user_id看起来像是模型级别的物理属性。 Let me know if this makes sense.
如果这是有道理的,请告诉我。
I think you are confused about rails 3 attr_accessible and attr_accessor . 我认为你对rails 3 attr_accessible和attr_accessor感到困惑。
Link about difference between them: https://stackoverflow.com/a/6958433/1306709 链接他们之间的差异: https : //stackoverflow.com/a/6958433/1306709
In rails 4 you still can use attr_accessor
, but if you meant attr_accessible
here is some info: strong parameters 在rails 4中你仍然可以使用
attr_accessor
,但是如果你的意思是attr_accessible
这里有一些信息: 强参数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.