gpt4 book ai didi

ruby-on-rails - Where(Hash) 后跟 ActiveRecord 中的 Scoped Where(Hash)

转载 作者:数据小太阳 更新时间:2023-10-29 08:13:36 25 4
gpt4 key购买 nike

简单地说:

class Project
scope :with_status_1, where(:status => 'test1')
scope :with_status_2, where('status = ?', 'test2')
end


Project.where(:status => 'test1').where(:status => 'test2')
Project Load (0.4ms) SELECT "projects".* FROM "projects" WHERE "projects"."status" = 'test1' AND "projects"."status" = 'test2'

Project.where(:status => 'test').with_status_1
Project Load (0.5ms) SELECT "projects".* FROM "projects" WHERE "projects"."status" = 'test1'

Project.where(:status => 'test').with_status_2
Project Load (0.5ms) SELECT "projects".* FROM "projects" WHERE "projects"."status" = 'test' AND "projects"."status" = 'test2'

Project.where('status = ?', 'test').with_status_1
Project Load (0.4ms) SELECT "projects".* FROM "projects" WHERE "projects"."status" = 'test' AND "projects"."status" = 'test1'

Project.with_status_1.where(:status => 'test')
Project Load (0.4ms) SELECT "projects".* FROM "projects" WHERE "projects"."status" = 'test1' AND "projects"."status" = 'test'

Project.with_status_1.where(:status => 'test').with_status_1
Project Load (0.4ms) SELECT "projects".* FROM "projects" WHERE "projects"."status" = 'test1'

为什么案例 #2 和案例 #6 会清除前面的条件?

编辑:Rails 3.1

最佳答案

where 子句将其 args 发送到 build_where

def build_where(opts, other = [])
case opts
when String, Array
[@klass.send(:sanitize_sql, other.empty? ? opts : ([opts] + other))]
when Hash
attributes = @klass.send(:expand_hash_conditions_for_aggregates, opts)
PredicateBuilder.build_from_hash(table.engine, attributes, table)
else
[opts]
end
end

所以在 Hash 的情况下,它将重写 key ,因为它将新的哈希选项附加到现有哈希。如果您传递的是 StringArray,它将对其求和。

关于ruby-on-rails - Where(Hash) 后跟 ActiveRecord 中的 Scoped Where(Hash),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7447201/

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