Rails通过belongs_to关联获取所有对象

时间:2014-12-27 20:10:29

标签: ruby-on-rails rails-activerecord

用户 has_many 问题

问题 belongs_to 用户

如何让问题的所有用户的“标记”列等于“ruby-on-rails”?

如果我Question.where(tag: "ruby-on-rails"),我会得到ActiveRecord::Relation

如果我Question.where(tag: "ruby-on-rails").users我收到错误undefined method 'users' for #<Question::ActiveRecord_Relation:0x007fbb96812aa0>

1 个答案:

答案 0 :(得分:7)

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