Elegantly selecting attributes from has_many :through join models in Rails

前端 未结 4 627
春和景丽
春和景丽 2021-02-06 14:51

I\'m wondering what the easiest/most elegant way of selecting attributes from join models in has_many :through associations is.

Lets say we have Items, Catalogs, and Cat

4条回答
  •  误落风尘
    2021-02-06 14:56

    You should be able to do @catalog.catalog_item.position if you provide the other end of the association.

    class Catalog < ActiveRecord::Base
      belongs_to :catalog_item
    end
    

    Now you can do Catalog.first.catalog_item.position.

提交回复
热议问题