这里仍然是新手,但我仍然无法使逻辑正确。
目前,我有:
我想补充一下:
我目前已经提出:
class User < ActiveRecord::Base
has_many :products
has_many :offered_prices
end
class Product < ActiveRecord::Base
belongs_to :user
has_many :offered_prices
end
这是我到目前为止所做的。它仍然看起来不太正确,因为我在同一时间感到困惑。非常感激您的帮忙! :)
答案 0 :(得分:2)
定义三个模型:
User | OfferedPrice | Product
他们之间的联系将是:
class User < ActiveRecord::Base
has_many :products
has_many :offered_prices, through: :products
end
class OfferedPrice < ActiveRecord::Base
belongs_to :user
belongs_to :product
# To make sure a user can offer price once for against a product
validates_uniqueness_of :price, scope: [:user, :product]
end
class Product < ActiveRecord::Base
has_many :offered_prices
has_many :user, through: :offered_prices
end