gpt4 book ai didi

mysql - Railed 数据库嵌套搜索混淆 : part 2

转载 作者:行者123 更新时间:2023-11-28 23:24:09 25 4
gpt4 key购买 nike

不久前我问过this question .

这是我的查询:

def self.search(search)
query = "%#{search}%"
if search
joins(:hobbies)
.where("user_name like ? or first_name like ? or hobbies.name like ? or hobbies.type like ?", query, query, query, query)
else
self.all
end
end

但是,我现在有第二个嵌套属性,我想按 -- 游戏进行搜索!

def self.search(search)
query = "%#{search}%"
if search
joins(:hobbies)
joins(:games)
.where("user_name like ? or first_name like ? or hobbies.name like ? or hobbies.type like ? or games.number like ?", query, query, query, query, query)
else
self.all
end
end

但这行不通。我究竟如何将它们连接在一起?

最佳答案

你漏掉了一个点。

def self.search(search)
query = "%#{search}%"
if search
joins(:hobbies)
.joins(:games)
.where("user_name like ? or first_name like ? or hobbies.name like ? or hobbies.type like ? or games.number like ?", query, query, query, query, query)
else
self.all
end
end

更好的写法是

def self.search(search)
query = "%#{search}%"
if search
joins(:hobbies, :games)
.where("user_name like ? or first_name like ? or hobbies.name like ? or hobbies.type like ? or games.number like ?", query, query, query, query, query)
else
self.all
end
end

关于mysql - Railed 数据库嵌套搜索混淆 : part 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40139218/

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