我正在尝试将以下 Rails where 子句转换为使用 Arel,主要是为了利用 Arel 提供的 or
方法。
发布模型
class Post
belongs_to :user
end
用户模型
class User
has_many :posts
end
我正在查找 Mark 发布的帖子。
这是 Rails 查询:
Post.joins(:user).where(users: { first_name: 'Mark' })
我需要使用 Arel 转换此查询。
提前致谢!
最佳答案
这应该可以。
# Generate Arel tables for both
posts = Arel::Table.new(:posts)
users = Arel::Table.new(:users)
# Make a join and add a where clause
posts.join(:users).on(posts[:user_id].eq(users[:id])).where(users[:first_name].eq('Mark'))
关于mysql - Rails Arel 通过连接表上的 where 条件进行连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39236293/