【问题标题】:Active Record find method with select带选择的 Active Record 查找方法
【发布时间】:2010-06-03 13:20:22
【问题描述】:

我目前似乎无法正确解决这个问题。

我想要不同的记录,但我需要其他属性与 find 方法的结果一起出现。当前非工作代码是:

Visit.find(:all, :select => "user_id, DISTINCT cookie")

【问题讨论】:

    标签: sql ruby-on-rails activerecord select find


    【解决方案1】:

    你必须在 :select 中写下你需要的所有列

    Visit.find(:all, :select => "DISTINCT cookie, user_id")
    

    但这会给出 cookie 和 user_id 的唯一组合

    喜欢

    cookie  user_id
    1          1
    1          2
    2          1
    2          2
    

    但如果你只想要两个 cookie,我认为你不能单独使用 Rails find 和 :select。另外,如果您愿意,它将代替上面示例中的“x”。

    cookie  user_id
    1          x
    2          x
    

    【讨论】:

      猜你喜欢
      • 2015-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      • 2013-01-01
      • 1970-01-01
      相关资源
      最近更新 更多