简体   繁体   中英

Rails - Named scopes

for my user's model I have:

class User < ActiveRecord::Base
  has_many :group_users, :class_name => "User", :finder_sql => 'select DISTINCT u.* from users u join permissions pp on pp.user_id=u.id join spaces p on pp.space_id=p.id where space_id in (select space_id from permissions pp2 where user_id=#{id}) and pp.user_id != #{id}'

How can I access that in a scope like:

  scope :usersInMySpaces, lambda { |user|
      group_users
  }

thanks

You can't and there's no point. has_many associations act like scopes in many cases but in this one the finder_sql is so complicated that it couldn't be combined with any other sql as a scope.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM