has_many 후 has_many : through?

Shpigford

내 현재 모델은 다음과 같습니다.

class Artist < ApplicationRecord
  has_many :albums
  has_many :follows
  has_many :users, -> { uniq }, through: :follows
end

class Album < ApplicationRecord
  belongs_to :artist
end

class Follow < ApplicationRecord
  belongs_to :artist
  belongs_to :user
end

class User < ApplicationRecord
  has_many :follows
  has_many :artists, -> { uniq }, through: :follows
end

내가 할 수 있기를 원하는 Albums것은 사용자를 위해 모든 것을 얻는 것 입니다.

아티스트를 쉽게 구할 수 @user.artists있지만 ( ), 그 아티스트의 앨범이 전부 다들 문제가 있어요.

Artists모델 Users통해 연결됩니다 Follows.

@users.albums또는 같은 것을 할 수 있기를 원합니다 @users.artists.albums.

디팍 마하 칼레

당신이 user has_many :artistsartist has_many :albums

모델 에서 has_many연결을 생성하십시오.Useralbum :through artists

class User < ApplicationRecord
  has_many :follows
  has_many :artists, -> { uniq }, through: :follows
  has_many :album, through: :artists
end

이제 사용할 수 있습니다 @user.albums

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서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

ActiveAdmin 필터 : has_many : through 속성

분류에서Dev

Can a has_many :through association have other associations?

분류에서Dev

Rails 3: has_many through controller action

분류에서Dev

Creating multiple has_many :through records with form_for

분류에서Dev

has_many : through로 사용자 찾기

분류에서Dev

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

분류에서Dev

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

분류에서Dev

has_many through가있는 곳

분류에서Dev

Ruby on Rails has_many : through in a polymorphic association

분류에서Dev

rails cancancan cancan has_many through abilities

분류에서Dev

has_many through-속성 액세스

분류에서Dev

has_many : through와 has_and_belongs_to_many 중 선택- "should"또는 "must"

분류에서Dev

ordering through an has_many relationship with a condition in rails 3 / active records / postgresql

분류에서Dev

has_many : through 잘못된 것 같습니다

분류에서Dev

Has_many, Through : Joiner 테이블 저장 실패

분류에서Dev

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

분류에서Dev

ActiveRecord has_many through inverse_of causing rails admin to work?

분류에서Dev

Having trouble adding view created from a has_many: through table.

분류에서Dev

How do I specify foreign_key and class when using has_many :through?

분류에서Dev

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

Related 관련 기사

  1. 1

    link to relationship name in has_many :through

  2. 2

    has_many : through 양식

  3. 3

    Rails has_many : through with the where 절

  4. 4

    Finding Users with has_many :through

  5. 5

    Make has_many :through not deleteable

  6. 6

    Join has_many :through attributes

  7. 7

    has_many / : through rails4

  8. 8

    Rails has_many : through, undefined method

  9. 9

    has_one : through 및 has_many : through

  10. 10

    ActiveAdmin 필터 : has_many : through 속성

  11. 11

    Can a has_many :through association have other associations?

  12. 12

    Rails 3: has_many through controller action

  13. 13

    Creating multiple has_many :through records with form_for

  14. 14

    has_many : through로 사용자 찾기

  15. 15

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

  16. 16

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

  17. 17

    has_many through가있는 곳

  18. 18

    Ruby on Rails has_many : through in a polymorphic association

  19. 19

    rails cancancan cancan has_many through abilities

  20. 20

    has_many through-속성 액세스

  21. 21

    has_many : through와 has_and_belongs_to_many 중 선택- "should"또는 "must"

  22. 22

    ordering through an has_many relationship with a condition in rails 3 / active records / postgresql

  23. 23

    has_many : through 잘못된 것 같습니다

  24. 24

    Has_many, Through : Joiner 테이블 저장 실패

  25. 25

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

  26. 26

    ActiveRecord has_many through inverse_of causing rails admin to work?

  27. 27

    Having trouble adding view created from a has_many: through table.

  28. 28

    How do I specify foreign_key and class when using has_many :through?

  29. 29

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

뜨겁다태그

보관