Rails has_many : through with the where 절

알렉스 스몰 로프

Rails에서 쉬운 Twitter 애플리케이션을 만들었습니다.

이제 나는 싶습니다 to choose three random users that are not followed by the current user.

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

class User < ActiveRecord::Base
    has_many :tweets, dependent: :destroy
    has_many :followerships, class_name: 'Followership', foreign_key: 'followed_id'
    has_many :followedships, class_name: 'Followership', foreign_key: 'follower_id'
    has_many :followers, through: :followerships, source: :follower
    has_many :followed, through: :followedships, source: :followed
end

class Followership < ActiveRecord::Base
    belongs_to :follower, class_name: "User"
    belongs_to :followed, class_name: "User"
    validates :follower_id, presence: true
    validates :followed_id, presence: true
end

class Tweet < ActiveRecord::Base
  belongs_to :user
end

다음 쿼리를 사용하려고했습니다.

User.where.not(followers: current_user).order("RANDOM()").limit(3)

그러나 no such column: users.follower_id오류가 발생 하면 분명히 작동하지 않습니다 .

SQL 쿼리 없이도 가능합니까?

감사합니다!

앤드류 웨이

이 시도:

already_following = current_user.followed.map(&:id)
@users = User.where.not(id: already_following).order("RANDOM()").limit(3)

기본적으로 내가 한 일은 이미 팔로우중인 사용자 목록을 얻었습니다. 그런 다음 사용자 테이블에서 ID가 이미 팔로우중인 사용자와 일치하지 않는지 확인합니다.

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

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

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

has_many, through, class_name 및 where 절이있는 Rails 모델 연결

분류에서Dev

Rails 4 has_one through with where 절

분류에서Dev

has_many / : through rails4

분류에서Dev

Rails has_many : through, undefined method

분류에서Dev

where 절을 통해 has_many 및 has_many

분류에서Dev

Rails 3: has_many through controller action

분류에서Dev

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

분류에서Dev

Ruby on Rails has_many : through in a polymorphic association

분류에서Dev

rails cancancan cancan has_many through abilities

분류에서Dev

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

분류에서Dev

ActiveRecord has_many through inverse_of causing rails admin to work?

분류에서Dev

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

분류에서Dev

has_many : through in Rails로 생성 및 업데이트

분류에서Dev

Rails 4 : : has_many, : through 및 테이블 이름

분류에서Dev

Ruby on Rails 'has_many : through', 데이터 저장

분류에서Dev

Rails 자체 참조 has_many through : is not added the right record

분류에서Dev

Rails 5.1 has_many through-관련 필드

분류에서Dev

has_many 후 has_many : through?

분류에서Dev

Rails는 has_many : through 관계에서 데이터를 생성합니다.

분류에서Dev

has_many : through 관계에서 Rails NameError 초기화되지 않은 상수

분류에서Dev

has_many : through 관계에서 Rails NameError 초기화되지 않은 상수

분류에서Dev

has_many through : in Rails를 사용하여 현재 및 이전 관계 쿼리

분류에서Dev

중첩 된 속성에 대한 양식 도우미 has_many through in rails 4

분류에서Dev

Rails는`has_many : through` 텍스트 필드를 통해 검색합니다.

분류에서Dev

Rails has_many : through "오류-products.category 열이 존재하지 않습니다"

분류에서Dev

link to relationship name in has_many :through

분류에서Dev

has_many : through 양식

분류에서Dev

Finding Users with has_many :through

분류에서Dev

Make has_many :through not deleteable

Related 관련 기사

  1. 1

    has_many, through, class_name 및 where 절이있는 Rails 모델 연결

  2. 2

    Rails 4 has_one through with where 절

  3. 3

    has_many / : through rails4

  4. 4

    Rails has_many : through, undefined method

  5. 5

    where 절을 통해 has_many 및 has_many

  6. 6

    Rails 3: has_many through controller action

  7. 7

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

  8. 8

    Ruby on Rails has_many : through in a polymorphic association

  9. 9

    rails cancancan cancan has_many through abilities

  10. 10

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

  11. 11

    ActiveRecord has_many through inverse_of causing rails admin to work?

  12. 12

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

  13. 13

    has_many : through in Rails로 생성 및 업데이트

  14. 14

    Rails 4 : : has_many, : through 및 테이블 이름

  15. 15

    Ruby on Rails 'has_many : through', 데이터 저장

  16. 16

    Rails 자체 참조 has_many through : is not added the right record

  17. 17

    Rails 5.1 has_many through-관련 필드

  18. 18

    has_many 후 has_many : through?

  19. 19

    Rails는 has_many : through 관계에서 데이터를 생성합니다.

  20. 20

    has_many : through 관계에서 Rails NameError 초기화되지 않은 상수

  21. 21

    has_many : through 관계에서 Rails NameError 초기화되지 않은 상수

  22. 22

    has_many through : in Rails를 사용하여 현재 및 이전 관계 쿼리

  23. 23

    중첩 된 속성에 대한 양식 도우미 has_many through in rails 4

  24. 24

    Rails는`has_many : through` 텍스트 필드를 통해 검색합니다.

  25. 25

    Rails has_many : through "오류-products.category 열이 존재하지 않습니다"

  26. 26

    link to relationship name in has_many :through

  27. 27

    has_many : through 양식

  28. 28

    Finding Users with has_many :through

  29. 29

    Make has_many :through not deleteable

뜨겁다태그

보관