To_label on second order association

Hi All,

I can’t figure out how to call the to_label method on a second order
association (AS 2.3, ROR 2.38). I have “Articles” that belongs to
“Rooms” that belongs to “Floors”. I want to show “Floors.description -
Rooms.description” as the label for Rooms inside Articles, and I also
want to use that label inside a select in the update/create for
Articles, the problem is that I get instead the ID of Floors, not the
outcome of the to_label method for the Floor object. I tried to call
explicitlily the to_label method on the associated object but I get a
nil class error.
In the end I’m giving up, whatever I do I cannot find a way to get it.
Here follows the details.

class Article < ActiveRecord::Base
belongs_to :room

class Room < ActiveRecord::Base
belongs_to :floor
def to_label
“#{floor} - #{description}” // calling explicitily floor.to_label
gives a nil class error

class Floor < ActiveRecord :: Base
has_many :rooms
def to_label


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs