在带有连接的 where 子句中传递符号和字符串有什么区别
what's the difference between by passing a symbol and string in where clause with joins
我有两个模型 User 和 Book。用户有用户名和电子邮件字段,书籍有作者和标题字段。当我使用连接根据关联数据以及在 where 子句中传递的条件获取用户时,我得到了不同的结果
当我运行这个
User.joins(:books).where('author = ? ','xxxx')
Mysql 生成的查询是:
"SELECT `users`.* FROM `users` INNER JOIN `books` ON `books`.`user_id` = `users`.`id` WHERE (author = 'xxxxx' )"
给我的用户的书包括 xxxx 作者而
当我运行这个
User.joins(:books).where(author: 'xxxx')
Mysql 生成的查询是:
"SELECT `users`.* FROM `users` INNER JOIN `books` ON `books`.`user_id` = `users`.`id` WHERE `users`.`author` = 'xxxx'">
给出 Mysql2::Error:'where clause' 中的未知列 'users.author':
SELECT `users`.* FROM `users` INNER JOIN `books` ON `books`.`user_id` = `users`.`id` WHERE `users`.`author` = 'xxxx'
ActiveRecord::StatementInvalid: Mysql2::Error: 'where clause' 中的未知列 'users.author': SELECT users
.* FROM users
INNER JOIN book
ON book
.user_id
= users
.id
WHERE users
.author
= 'Dan Brown'
我的问题: 是当我在 where 子句中传递一个字符串封装字段和值时它给我结果但是当我在 where 子句中传递一个符号时它给我 mysql 未知列错误。那么ruby解释器如何知道从哪里获取符号传递和字符串传递中的数据
User.joins(:books).where('author = ? ','xxxx')
没有受到 sql 注入,但 User.joins(:books).where(author: 'xxxx')
没有。
最好使用User.joins(:books).where('author = ? ','xxxx')
,因为它更容易编写更复杂的 where 子句。
当你写 User.joins(:books).where(author: 'xxxx')
rails 解释器将尝试解释为 users.author = 'xxxx'
您可以查看 link 了解更多信息。
当我们加入 tables 时,尤其是在 rails 中,rails 有权使用加入的 tables 的别名。
您的查询,
User.joins(:books).where(author: 'xxxx')
在 用户中搜索作者字段 table
User.joins(:books).where(author: 'xxxx')
总是取第一个table和
的字段
User.joins(:books).where('author = ? ','xxxx')
在 table
中搜索
例如,User.joins(:books).where('id = ? ','xxxx')
,试试这个查询。
在上面你会得到一个错误,因为 id
字段存在于两个 table 中并且它变得混乱
但是,
User.joins(:books).where(id: 'xxxx')
有效,因为它仅在用户 table.
中搜索
所以,你可以使用,通过书籍引用作者 books: {author: 'xxxx'}
User.joins(:books).where(books: {author: 'xxxx'}) // your required query
或
User.joins(:books).where('author = ? ','xxxx')
我有两个模型 User 和 Book。用户有用户名和电子邮件字段,书籍有作者和标题字段。当我使用连接根据关联数据以及在 where 子句中传递的条件获取用户时,我得到了不同的结果
当我运行这个
User.joins(:books).where('author = ? ','xxxx')
Mysql 生成的查询是:
"SELECT `users`.* FROM `users` INNER JOIN `books` ON `books`.`user_id` = `users`.`id` WHERE (author = 'xxxxx' )"
给我的用户的书包括 xxxx 作者而
当我运行这个
User.joins(:books).where(author: 'xxxx')
Mysql 生成的查询是:
"SELECT `users`.* FROM `users` INNER JOIN `books` ON `books`.`user_id` = `users`.`id` WHERE `users`.`author` = 'xxxx'">
给出 Mysql2::Error:'where clause' 中的未知列 'users.author':
SELECT `users`.* FROM `users` INNER JOIN `books` ON `books`.`user_id` = `users`.`id` WHERE `users`.`author` = 'xxxx'
ActiveRecord::StatementInvalid: Mysql2::Error: 'where clause' 中的未知列 'users.author': SELECT users
.* FROM users
INNER JOIN book
ON book
.user_id
= users
.id
WHERE users
.author
= 'Dan Brown'
我的问题: 是当我在 where 子句中传递一个字符串封装字段和值时它给我结果但是当我在 where 子句中传递一个符号时它给我 mysql 未知列错误。那么ruby解释器如何知道从哪里获取符号传递和字符串传递中的数据
User.joins(:books).where('author = ? ','xxxx')
没有受到 sql 注入,但 User.joins(:books).where(author: 'xxxx')
没有。
最好使用User.joins(:books).where('author = ? ','xxxx')
,因为它更容易编写更复杂的 where 子句。
当你写 User.joins(:books).where(author: 'xxxx')
rails 解释器将尝试解释为 users.author = 'xxxx'
您可以查看 link 了解更多信息。
当我们加入 tables 时,尤其是在 rails 中,rails 有权使用加入的 tables 的别名。
您的查询,
User.joins(:books).where(author: 'xxxx')
在 用户中搜索作者字段 table
User.joins(:books).where(author: 'xxxx')
总是取第一个table和
User.joins(:books).where('author = ? ','xxxx')
在 table
例如,User.joins(:books).where('id = ? ','xxxx')
,试试这个查询。
在上面你会得到一个错误,因为 id
字段存在于两个 table 中并且它变得混乱
但是,
User.joins(:books).where(id: 'xxxx')
有效,因为它仅在用户 table.
所以,你可以使用,通过书籍引用作者 books: {author: 'xxxx'}
User.joins(:books).where(books: {author: 'xxxx'}) // your required query
或
User.joins(:books).where('author = ? ','xxxx')