在 Rails 中,我们有这样的has_many
特性:
class Product < ApplicationRecord
has_many :product_sales
has_many :states, through: :product_sales
end
有什么办法可以给其中一个自定义名称has_many
?
例如:而不是访问states
从Product
使用@product.states
,我想通过访问它@product.states_where_it_is_sold
。
是的,有办法。做:
class Product < ApplicationRecord
has_many :product_sales
has_many :states_where_sold, through: :product_sales, source: :state
end
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句