你如何在Rails 3 ActiveRecord中进行OR查询.我找到的所有示例都只有AND查询.
编辑:自Rails 5以来可以使用OR方法.请参阅ActiveRecord :: QueryMethods
我用Ruby on Rail的查询接口写了几个复杂的查询(至少对我来说):
watched_news_posts = Post.joins(:news => :watched).where(:watched => {:user_id => id})
watched_topic_posts = Post.joins(:post_topic_relationships => {:topic => :watched}).where(:watched => {:user_id => id})
Run Code Online (Sandbox Code Playgroud)
这两个查询都可以自行完成.两者都返回Post对象.我想将这些帖子合并到一个ActiveRelation中.由于某些时候可能有数十万个帖子,因此需要在数据库级别完成.如果是MySQL查询,我可以简单地使用UNION运算符.有没有人知道我是否可以用RoR的查询界面做类似的事情?
我知道有三个主要的符号为whereActiveRecord方法提供参数:
指定and的where方法是直截了当:
# Pure String notation
Person.where("name = 'Neil' AND age = 27")
# Array notation
Person.where(["name = ? AND age = ?", 'Neil', 27])
# Hash notation
Person.where({name: "Neil", age: 27})
Run Code Online (Sandbox Code Playgroud)
or为这个相同的where方法指定是为了哈希语法.可能吗?
# Pure String notation
Person.where("name = 'Neil' OR age = 27")
# Array notation
Person.where(["name = ? OR age = ?", 'Neil', 27])
# Hash notation DOESN'T WORK
Person.where({name: "Neil" OR age: 27})
Run Code Online (Sandbox Code Playgroud) activerecord ruby-on-rails ruby-on-rails-4 rails-activerecord ruby-on-rails-5
我想找到所有Annotations身体都是:
最好的方法是什么?
我想尽可能使用SearchLogic,但是SearchLogic允许您执行以下各项操作:
Annotation.body_equals('?')Annotation.body_like('[?]')你总是可以将它们连在一起: Annotation.body_equals('?').body_like('[?]')
我不确定如何将它们结合起来OR.
请注意,如果命名范围与参数相同,OR 则可以将它们组合在一起.我可以这样做:
Annotation.body_equals_or_body_like('?')
Run Code Online (Sandbox Code Playgroud)
但这没有用.
请注意,我没有附加到SearchLogic,但对于不需要破坏其抽象的解决方案来说,它会很棒.
class Guardian < ActiveRecord::Base
has_many :patients
has_one :user, as: :profile
accepts_nested_attributes_for :user
end
class User < ActiveRecord::Base
belongs_to :profile, :polymorphic => true
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
Run Code Online (Sandbox Code Playgroud)
用户迁移
class DeviseCreateUsers < ActiveRecord::Migration
def change
create_table(:users) do |t|
## Database authenticatable
t.string :email, :null => false, :default => ""
t.string :encrypted_password, :null => false, :default => ""
t.string :username, :null => false
t.string :address
t.integer :age
t.string :gender
t.string :name
t.integer :profile_id
t.string :profile_type
## Recoverable
t.string :reset_password_token …Run Code Online (Sandbox Code Playgroud) 我正在尝试合并两个范围或向现有范围添加更多内容。
scope :public_bids, -> {
where(status: [Status::ACCEPTED, Status::OUTBID, Status::SOLD, Status::NO_SALE],
bid_type: [BidType::MANUAL, BidType::AUTO, BidType::LIVE])
scope :outbid_maxbids, -> {
where(status: Status::OUTBID, bid_type: BidType::MAXBID)
Run Code Online (Sandbox Code Playgroud)
我无法确定如何将OR它们组合在一起或合并到一个范围中。有什么建议/指导吗?我更希望将它们合并到一个范围中。
在Rails3中我有:
Class Teacher
# active :boolean
has_and_belongs_to_many :subjects
Class Subject
# active :boolean
has_and_belongs_to_many :teachers
Run Code Online (Sandbox Code Playgroud)
我正在尝试构建一个教师范围,它返回与之相关的所有Teachers内容active或与之相关联的Subject内容active.
这些范围单独工作,但如何将它们作为单个范围与OR组合?
scope :active_teachers, where(active: true)
scope :more_active_teachers, joins(:subjects).where(:subjects => {active: true})
Run Code Online (Sandbox Code Playgroud)
我试过这个没有成功:
scope :active_teachers, where(active: true).or(joins(:subjects)
.where(:subjects => {active: true}))
Run Code Online (Sandbox Code Playgroud)
更新:
我以为我有一个解决方案,但这不再是懒惰的负载,两次点击数据库 - 最重要的是 - 返回一个数组而不是一个AR对象!
scope :active_teachers, where(active: true) |
joins(:subjects).where(:subjects => {active: true})
Run Code Online (Sandbox Code Playgroud)