0

此方法从数据库返回一些记录。

   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)))

请问我该怎么做?

4

1 回答 1

1

这很容易 =)

只需将您想要的条件包含在引号中:

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 模型,您必须传递真实的表名!(但据我所知,这是您的表名.. 不是很方便,顺便说一句)

于 2013-07-19T11:57:08.947 回答