Ruby on rails 如何自定义Rails中多个关联的间接链接的名称?
在Rails中,我们有很多功能:Ruby on rails 如何自定义Rails中多个关联的间接链接的名称?,ruby-on-rails,database,has-many-through,has-many,Ruby On Rails,Database,Has Many Through,Has Many,在Rails中,我们有很多功能: class Product < ApplicationRecord has_many :product_sales has_many :states, through: :product_sales end 类产品
class Product < ApplicationRecord
has_many :product_sales
has_many :states, through: :product_sales
end
类产品
我有没有办法给其中一个有很多的命名
例如:我不想使用@Product.states
从产品
中访问状态
,而是想使用@Product.states\u销售地点
是的,有一种方法。做:
class Product < ApplicationRecord
has_many :product_sales
has_many :states_where_sold, through: :product_sales, source: :state
end
类产品
是的,有办法。做:
class Product < ApplicationRecord
has_many :product_sales
has_many :states_where_sold, through: :product_sales, source: :state
end
类产品
是的,肯定有办法做到这一点。我相信您想查看本手册的第4.3.2.9节。而且,也许只有@product.states\u销售地点
,但这是个人偏好的问题。@jvillian,谢谢你的回答:D。我试过:有很多:states,通过::product\u sales,source::states\u销售地点
,但它不起作用=/Trysource::state
source
应该参考ProductSale
上的所属:state
字段。不管怎样,我只是用有很多:states\u在哪里销售,通过::product\u sales,source::state
。请你写一个答案,这样我可以把它设置为正确的,好吗?是的,肯定有办法做到这一点。我相信您想查看本手册的第4.3.2.9节。而且,也许只有@product.states\u销售地点
,但这是个人偏好的问题。@jvillian,谢谢你的回答:D。我试过:有很多:states,通过::product\u sales,source::states\u销售地点
,但它不起作用=/Trysource::state
source
应该参考ProductSale
上的所属:state
字段。不管怎样,我只是用有很多:states\u在哪里销售,通过::product\u sales,source::state
。请你写一个答案好让我把它设置为正确的吗?太好了!它可以\o/工作。非常感谢,@jvillian!好极了!它可以\o/工作。非常感谢,@jvillian!