I have an apartment class with an attr_accessor called checkin (doesn't need to be saved in 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
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. Why am I getting nil when calling from bed? Thanks to all helpers!
You said you were calling it from
bed
- as inbed.apartment.checkin
?If that is true, that is your problem: you only set
checkin
on the Apartment instance saved in the instance variable@apartment
. That one is not identical with the instance you receive throughbed.apartment.checkin
.