gpt4 book ai didi

ruby-on-rails - Rails ActiveRecord::find Join语句问题

转载 作者:行者123 更新时间:2023-12-02 04:18:44 25 4
gpt4 key购买 nike

为什么下面的Rails声明

User.find(:all, :joins => [:roles,:roles_users], 
:conditions => { :roles => { :name => 'subscriber',
:authorizable_id => self.id,
:authorizable_type => self.class.to_s }})

转换为此(使用2x相同的连接)
SELECT "users".* FROM "users" 
JOIN "roles_users" ON ("users"."id" = "roles_users"."user_id")
JOIN "roles" ON ("roles"."id" = "roles_users"."role_id")
JOIN "roles_users" roles_users_users ON roles_users_users.user_id = users.id
WHERE ("roles"."authorizable_id" = 4 AND "roles"."name" = 'subscriber' AND "roles"."authorizable_type" = 'Howto')

只是好奇。

最佳答案

我不需要加入Roles_users,因为该插件已经做了一次...

谢谢你的帮助。

has_many :users, :finder_sql => 'SELECT DISTINCT users.* FROM users INNER JOIN roles_users ON user_id = users.id INNER JOIN roles ON roles.id = role_id WHERE authorizable_type = \'#{self.class.base_class.to_s}\' AND authorizable_id = #{id}', :counter_sql => 'SELECT COUNT(DISTINCT users.id) FROM users INNER JOIN roles_users ON user_id = users.id INNER JOIN roles ON roles.id = role_id WHERE authorizable_type = \'#{self.class.base_class.to_s}\' AND authorizable_id = #{id}', :readonly => true

关于ruby-on-rails - Rails ActiveRecord::find Join语句问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1001789/

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