This is my posting model. Posts have many user_ids through postings. I'd like to return the Posts that have user_id 2 and 3. So posts with ID 7 and 8.
Ideally my code would look something like this... but it doesn't work
Post.joins(:postings).where("postings.user_id = ?", [2,3])
id | post_id | user_id | created_at | updated_at |
+---+---------+--------+---------------------------+---------------------------+
| 1 | 7 | 1 | 2013-09-07 16:03:50 -0400 | 2013-09-07 16:03:50 -0400 |
| 2 | 7 | 2 | 2013-09-07 16:03:50 -0400 | 2013-09-07 16:03:50 -0400 |
| 3 | 7 | 3 | 2013-09-07 16:03:50 -0400 | 2013-09-07 16:03:50 -0400 |
| 4 | 8 | 2 | 2013-09-07 22:17:49 -0400 | 2013-09-07 22:17:49 -0400 |
| 5 | 8 | 3 | 2013-09-07 22:17:49 -0400 | 2013-09-07 22:17:49 -0400 |
| 6 | 8 | 6 | 2013-09-07 22:17:49 -0400 | 2013-09-07 22:17:49 -0400 |
| 7 | 12 | 3 | 2013-09-14 12:49:56 -0400 | 2013-09-14 12:49:56 -0400 |
ADDED COMMENTS
- I am using Rails
I'd like to be able to keep the [2,3] dynamic so there can be 10 entries in the array or 2
the Post table does not have a user_id column. The Posting model has a user_id column and the User table has a id column. The relationship between the two has been setup using a has_many relationship.