gpt4 book ai didi

mysql - Rails 一对多关联查询

转载 作者:行者123 更新时间:2023-11-29 20:21:36 24 4
gpt4 key购买 nike

型号:

class User < ActiveRecord::Base
has_may :friends
end

class Friend < ActiveRecord::Base
belongs_to :user
end

表格:

tables image

查询:

用户两者都具有:

friends.name = "Lily" 
# and
friends.name = "Steven"

我想要通过模型查询,例如:

User.joins(xxx).where(xxx)

有什么办法吗?

最佳答案

我认为

User.joins(:friends).where("friends.name = 'Lily' OR friends.name = 'Steven'")

会做你正在寻找的事情

关于mysql - Rails 一对多关联查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39481744/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com