此方法从数据库返回一些记录。
def wall_record(ref_id,followers_record,pid,sd_pid)
@Wp_rec=Wallpost.select('Wp.id as Pid,Wp.*,Wi.id as Iid,Wi.imagename,Wv.videourl,U.firstname,U.username')
@Wp_rec=@Wp_rec.where('Wp.id > ? ', pid) if pid.present? # pid->PostId
@Wp_rec=@Wp_rec.where('Wp.id < ? ', sd_pid) if sd_pid.present? # sd_pid -> Scroll down
@Wp_rec=@Wp_rec.where('Wp.posted_by IN (?) ', followers_record) if followers_record.present?
@Wp_rec=@Wp_rec.joins('Wp INNER JOIN `epoker_wallimages` as Wi on Wi.wallpost_id = Wp.id')
@Wp_rec=@Wp_rec.joins('INNER JOIN `epoker_users` as U on U.id = Wp.user_id')
@Wp_rec=@Wp_rec.joins('INNER JOIN `epoker_wallvideos` as Wv on Wv.wallpost_id = Wp.id')
@Wp_rec=@Wp_rec.order('Wp.id DESC') if followers_record.present?
@Wp_rec=@Wp_rec.limit(5) #if followers_record.present?
end
现在我想添加mysql查询
-> where((Wp.posted_by != 100000 AND post_status = 1) OR (Wp.posted_by = 10000 AND post_status in (1,2)))
请问我该怎么做?
最佳答案
很简单=)
只需将您想要的条件包含在引号中即可:
where("(Wp.posted_by != 100000 AND post_status = 1) OR (Wp.posted_by = 10000 AND post_status in (1,2))")
您甚至可以在引号内传递任何您想要的参数
where("(Wp.posted_by != 100000 AND post_status = #{post_status}) OR (Wp.posted_by = #{posted_by} AND post_status in (#{post_statuses_array.join(',')}))")
最后,您可以使用更像 Rails 的方法来实现
where("(Wp.posted_by ? != AND post_status = ?) OR (Wp.posted_by = ? AND post_status in (?))",100000, 1, 10000, [1,2])
您必须传递真实的表名称,而不是 Wp 模型!(但据我所知,这是您的表名称..顺便说一句,不太方便)
关于mysql - 在 ruby on Rails 中追加查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17745007/