Is it possible for a has_many, through: assocation joined model to have associations ? Lets say have this association where I have a list of products that has a lot of different prices where each price is linked to a country. So I can either get all the countries in which my product is available or each products available within a country.
products
has_many :prices
has_many :countries, through :prices
prices
belongs_to :product
belongs_to :country
countries
has_many :prices
has_many :products, through :prices
Now I want to add other associations on my price model, where prices has_one :currency, and where the price is defined by a user to which this information belongs. Is it possible to add association like this ?
prices
belongs_to :product
belongs_to :country
has_one :currency
belongs_to :user
I don't know if a simple "yes" will help you with your question, but (since you basically already provide the correct code): Yes.
Your prices
join model is a default Rails model, so you can treat it like your other models (and implement associations, callbacks, custom finders, etc.). You just need to make sure that the foreign keys in your database tables line up correctly (prices
needs a user_id
column for example).
Not really part of the question, but a small remark:
I'm not sure that you want your prices to has_one
currency though. This implies that the foreign key is in the currencies
table (price_id
) which in turn means that a currency belongs_to
a price (maybe you rather want your currency to has_many
prices and your price also belongs_to
a currency).
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다