Can a has_many :through association have other associations?

user3805610

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
Michael Trojanek

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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

FactoryGirl: has_many :through, validates_presence_of: association error: can't be blank?

분류에서Dev

NoMethodError: undefined method `user' for has_many :through Association

분류에서Dev

Ruby on Rails has_many : through in a polymorphic association

분류에서Dev

Rails 4 활성 레코드 모델 has_many through associations?

분류에서Dev

활성 레코드 연결-오류 w has_many : through Association?

분류에서Dev

Rails 4: has many through association error

분류에서Dev

has_many 후 has_many : through?

분류에서Dev

레일 위의 루비 has_many associations

분류에서Dev

Rails 4 has_many : through association : 다른 부모 모델에서 Devise current_user 사용 after_create 콜백

분류에서Dev

link to relationship name in has_many :through

분류에서Dev

has_many : through 양식

분류에서Dev

Rails has_many : through with the where 절

분류에서Dev

Finding Users with has_many :through

분류에서Dev

Make has_many :through not deleteable

분류에서Dev

Join has_many :through attributes

분류에서Dev

has_many / : through rails4

분류에서Dev

Rails has_many : through, undefined method

분류에서Dev

has_one : through 및 has_many : through

분류에서Dev

ActiveRecord has_many association NameError: uninitialized constant

분류에서Dev

Ajax load instances from a has_many association

분류에서Dev

Rails Association Tertiary has_many belongs_to

분류에서Dev

ActiveAdmin 필터 : has_many : through 속성

분류에서Dev

Rails 3: has_many through controller action

분류에서Dev

Creating multiple has_many :through records with form_for

분류에서Dev

has_many : through로 사용자 찾기

분류에서Dev

ActiveRecord, Rails 4 : has_many : through with scoped conditions failure

분류에서Dev

has_many through가있는 곳

분류에서Dev

rails cancancan cancan has_many through abilities

분류에서Dev

has_many through-속성 액세스

Related 관련 기사

  1. 1

    FactoryGirl: has_many :through, validates_presence_of: association error: can't be blank?

  2. 2

    NoMethodError: undefined method `user' for has_many :through Association

  3. 3

    Ruby on Rails has_many : through in a polymorphic association

  4. 4

    Rails 4 활성 레코드 모델 has_many through associations?

  5. 5

    활성 레코드 연결-오류 w has_many : through Association?

  6. 6

    Rails 4: has many through association error

  7. 7

    has_many 후 has_many : through?

  8. 8

    레일 위의 루비 has_many associations

  9. 9

    Rails 4 has_many : through association : 다른 부모 모델에서 Devise current_user 사용 after_create 콜백

  10. 10

    link to relationship name in has_many :through

  11. 11

    has_many : through 양식

  12. 12

    Rails has_many : through with the where 절

  13. 13

    Finding Users with has_many :through

  14. 14

    Make has_many :through not deleteable

  15. 15

    Join has_many :through attributes

  16. 16

    has_many / : through rails4

  17. 17

    Rails has_many : through, undefined method

  18. 18

    has_one : through 및 has_many : through

  19. 19

    ActiveRecord has_many association NameError: uninitialized constant

  20. 20

    Ajax load instances from a has_many association

  21. 21

    Rails Association Tertiary has_many belongs_to

  22. 22

    ActiveAdmin 필터 : has_many : through 속성

  23. 23

    Rails 3: has_many through controller action

  24. 24

    Creating multiple has_many :through records with form_for

  25. 25

    has_many : through로 사용자 찾기

  26. 26

    ActiveRecord, Rails 4 : has_many : through with scoped conditions failure

  27. 27

    has_many through가있는 곳

  28. 28

    rails cancancan cancan has_many through abilities

  29. 29

    has_many through-속성 액세스

뜨겁다태그

보관