Issue
In Rails, we have the has_many feature:
class Product < ApplicationRecord
has_many :product_sales
has_many :states, through: :product_sales
end
Is there any way I can give a custom name to one of these has_manys?
For example: instead of accessing states from Product by using @product.states, I would like to access it using @product.states_where_it_is_sold.
Solution
Yes, there is a way. Do:
class Product < ApplicationRecord
has_many :product_sales
has_many :states_where_sold, through: :product_sales, source: :state
end
Answered By - jvillian Answer Checked By - Robin (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.