attr_accessor of one class returns nil in associated class

560 views Asked by At

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!

1

There are 1 answers

3
janfoeh On BEST ANSWER

You said you were calling it from bed - as in 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. That one is not identical with the instance you receive through bed.apartment.checkin.