gpt4 book ai didi

mysql - Rails 控制台不同的 MySQL 结果

转载 作者:行者123 更新时间:2023-11-29 13:17:39 27 4
gpt4 key购买 nike

我在 Rails 中使用 LEFT OUTER JOIN 时遇到问题。我想我已经有了 Ruby 代码,但是当我将其输入到 Rails 控制台时,我得到一个结果,而当我复制生成的 MySQL 时,我得到另一个结果。 rails 控制台 中缺少整个列(值)。为什么 SQL 完全相同但结果不同?理想情况下,我希望结果包含值列。一如既往,任何帮助将不胜感激。

Rails 控制台响应

2.0.0-p353 :023 > SecurityItem.joins("LEFT OUTER JOIN  security_role_permissions ON security_role_permissions.security_item_id = security_items.id  AND security_role_permissions.role_id ='1'").select("security_items.name, security_role_permissions.value")

SecurityItem Load (0.7ms) SELECT security_items.name, security_role_permissions.value FROM `security_items` LEFT OUTER JOIN security_role_permissions ON security_role_permissions.security_item_id = security_items.id AND security_role_permissions.role_id ='1

=> #<ActiveRecord::Relation
[#<SecurityItem id: nil, key: "ChangePassword", name: "Change Password">,
#<SecurityItem id: nil, key: "AddUsers", name: "Add Users">,
#<SecurityItem id: nil, key: "UpdateUsers", name: "Update Users">,
#<SecurityItem id: nil, key: "DeleteUsers", name: "Delete Users">]>

MySQL 响应

mysql>  SELECT security_items.name, security_role_permissions.value FROM `security_items` LEFT OUTER JOIN security_role_permissions ON security_role_permissions.security_item_id = security_items.id AND security_role_permissions.role_id ='1';

+-----------------+-------+
| name | value |
+-----------------+-------+
| Change Password | 1 |
| Add Users | NULL |
| Update Users | NULL |
| Delete Users | NULL |
+-----------------+-------+

最佳答案

只有来自 SecurityItem 类的属性才会出现在返回结果中。但是,您可以通过“attributes”方法访问连接表中的属性。

security_items = SecurityItem.joins("LEFT OUTER JOIN  security_role_permissions 
ON security_role_permissions.key = security_items.key AND
security_role_permissions.role_id ='1'").select("security_items.name,
security_items.key, security_role_permissions.permission")

security_items.each { |security_item| puts security_item.attributes['permission'] }

关于mysql - Rails 控制台不同的 MySQL 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21268821/

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