Search code examples
rubyactiverecordrelational-databasebelongs-to

Table.where(x:y).othertable


Is there some way using my relational database setup with active record to get a group of x and find all of their y's assuming that each y belong_to an x and each x has_many y's?


Solution

  • It should not be done this way at all. You need to join the tables together lke this.

    It you have a class User that has_many Posts then you should write Post.joins(:user).where('users.x = ?', y)

    This way you should return all posts of users who have a certain attribute.