如何向SQL查询添加AND条件
我试图添加一个AND条件,其中如何向SQL查询添加AND条件,sql,ruby-on-rails,Sql,Ruby On Rails,我试图添加一个AND条件,其中active=true也包含在rails内部的SQL查询中 Schema.rb ... create_table 'relationships', force: :cascade do |t| t.integer 'follower_id' t.integer 'followed_id' t.datetime 'created_at', null: false t.datetime 'updated
active=true
也包含在rails内部的SQL查询中
Schema.rb
...
create_table 'relationships', force: :cascade do |t|
t.integer 'follower_id'
t.integer 'followed_id'
t.datetime 'created_at', null: false
t.datetime 'updated_at', null: false
t.boolean 'active', default: true
t.index ['followed_id'], name: 'index_relationships_on_followed_id'
t.index %w[follower_id followed_id], name: 'index_relationships_on_follower_id_and_followed_id', unique: true
t.index ['follower_id'], name: 'index_relationships_on_follower_id'
end
...
...
def feed
following_ids = "SELECT followed_id FROM relationships
WHERE follower_id = :user_id AND active = true"
View.where("user_id IN (#{following_ids})
OR user_id = :user_id", user_id: id).includes(:user)
end
...
user.rb
...
create_table 'relationships', force: :cascade do |t|
t.integer 'follower_id'
t.integer 'followed_id'
t.datetime 'created_at', null: false
t.datetime 'updated_at', null: false
t.boolean 'active', default: true
t.index ['followed_id'], name: 'index_relationships_on_followed_id'
t.index %w[follower_id followed_id], name: 'index_relationships_on_follower_id_and_followed_id', unique: true
t.index ['follower_id'], name: 'index_relationships_on_follower_id'
end
...
...
def feed
following_ids = "SELECT followed_id FROM relationships
WHERE follower_id = :user_id AND active = true"
View.where("user_id IN (#{following_ids})
OR user_id = :user_id", user_id: id).includes(:user)
end
...
但是,添加
和active=TRUE
似乎无法将跟随id
和跟随id
作为self
,其中active
布尔值为TRUE
通过在查询中分配active
来解决它
...
def feed
following_ids = "SELECT followed_id FROM relationships
WHERE follower_id = :user_id AND active = :active"
View.where("user_id IN (#{following_ids})
OR user_id = :user_id", user_id: id, active: true).includes(:user)
end
...