我有 2 个有has_and_belongs_to_many
关系的模型:
class Category < ActiveRecord::Base
has_and_belongs_to_many :templates
end
class Template < ActiveRecord::Base
has_and_belongs_to_many :categories
end
我想知道如何通过这种关系获得类别名称,例如我找到了第一个模板:
t = Template.find(:first)
然后使用t.categories
将返回一个对象,但我想有 category.name 作为回报,我该如何实现呢?