Rails gets all objects through the belongs_to association

User has_many Questions

The question belongs to_o User

How can I get all users for questions that have their "tag" column equal to "ruby-on-rails"?

If I do Question.where(tag: "ruby-on-rails")

, I get ActiveRecord::Relation

.

If I do Question.where(tag: "ruby-on-rails").users

I get the errorundefined method 'users' for #<Question::ActiveRecord_Relation:0x007fbb96812aa0>

+3


source to share


1 answer


User.joins(:questions).where(questions: {tag: "ruby-on-rails"})

      



+7


source







All Articles