[英]attr_accessor of one class returns nil in associated class
I have an apartment class with an attr_accessor called checkin (doesn't need to be saved in DB) 我有一个带有attr_accessor的单元类,称为checkin(不需要保存在DB中)
class Apartment < ActiveRecord::Base
attr_accessible :checkin
attr_accessor :checkin
has_many :rooms
has_many :beds through: rooms
end
In my controller i'm setting checkin's value 在我的控制器中,我正在设置签入值
def show
@apartment.update_attributes(checkin: session[:checkin])
end
When the page renders i call a method of bed which tries to access self.room.apartment.checkin
but i am getting nil value 当页面呈现时,我调用了试图访问self.room.apartment.checkin
的bed方法,但我却得到了nil值
This method is called by a method in apartment. 此方法由单元中的方法调用。 I've debbuged the call and when i am inside the apartment model the checkin variable has a value. 我已经对调用进行了调试,并且当我进入公寓模型时,checkin变量具有一个值。 Why am I getting nil when calling from bed? 为什么从床上打电话时我没有钱? Thanks to all helpers! 感谢所有帮助者!
You said you were calling it from bed
- as in bed.apartment.checkin
? 您说您是从bed
打来的-就像bed.apartment.checkin
?
If that is true, that is your problem: you only set checkin
on the Apartment instance saved in the instance variable @apartment
. 如果是这样,那就是您的问题:您仅在实例变量@apartment
保存的Apartment实例上设置checkin
。 That one is not identical with the instance you receive through bed.apartment.checkin
. 那与您通过bed.apartment.checkin
收到的实例bed.apartment.checkin
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.