neo neo - 1 year ago 106
MySQL Question

Rails Arel joins with where condition on joined table

I'm trying to convert the following Rails where clause to use Arel, mostly to take advantage of the

method that Arel provides.

Post model

Class Post
belongs_to :user

User model

class User
has_many :posts

I'm looking for posts posted by Mark.

This is the Rails Query:

Post.joins(:user).where(users: { first_name: 'Mark' })

I need to convert this query with Arel.

Thanks in advance!

Answer Source

This should do it.

# Generate AREL tables for both
posts =
users =

# Make a join and add where clause
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download