【发布时间】:2015-08-24 10:33:34
【问题描述】:
我有 3 个模型,
用户、位置、项目
Location 将只有 1 个用户,但 User 有许多项目或位置。和 Items 属于用户或位置。
class Location < ActiveRecord::Base
belongs_to :user
has_many items, through: :users
end
class User < ActiveRecord::Base
has_many :locations
has_many :items
end
class Item < ActiveRecord::Base
belongs_to :user
end
但是我收到了这个错误:
Could not find the association :users in model Location
我知道,我可以在 Location 模型中添加 has_many :users,但 location 应该只有 1 个用户。
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-4 activerecord