【问题标题】:How to translate this MySQL statement into named_scope method?如何将此 MySQL 语句转换为 named_scope 方法?
【发布时间】:2010-03-19 14:56:53
【问题描述】:

“select * from users, Awards where (users.id = Awards.user_id) and Awards.trophy_id not in (select Awards.trophy_id from Awards where Awards.trophy_id = #{trophy.id})”

【问题讨论】:

  • 这是做什么的?命名范围用于单个模型;你想获得用户还是奖励?此外,内部 select 语句看起来总是只返回 trophy.id

标签: sql ruby-on-rails ruby named-scope


【解决方案1】:

回答了自己的问题:

named_scope :not_awarded_trophy, lambda { |trophy| { :include => :awards, :conditions => [ "awards.trophy_id 不在(从 Awards.trophy_id = ?) 中选择 Awards.trophy_id", trophy.id ] } }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-05
    相关资源
    最近更新 更多