【发布时间】:2020-10-06 21:33:37
【问题描述】:
我有以下型号:
class Restaurant < ApplicationRecord
has_one_attached :image
has_many :categories, through: :restaurant_category
end
class Category < ApplicationRecord
has_many :restaurants, through: :restaurant_category
end
class RestaurantCategory < ApplicationRecord
belongs_to :restaurant
belongs_to :category
end
我会一次性查询与餐厅相关的所有类别。像这样的东西:
a = Restaurant.find(1)
a.restaurant_category
但我有:
NoMethodError (undefined method `restaurant_category' for #<Restaurant:0x00007f5214ad2240>)
我该如何解决?
【问题讨论】:
标签: ruby-on-rails activerecord many-to-many associations