Ruby on rails - 哪里有SQL方法

时间:2012-08-10 14:35:24

标签: sql ruby-on-rails ruby-on-rails-3 activerecord

我希望得到'某事'=真的所有微博。

此代码可以正常使用

class UsersController < ApplicationController
  .
  .
  .
  def show
    @user = User.find(params[:id])
    @microposts = @user.microposts
    @titre = @user.nom
  end
end

但是当我尝试使用where sql方法时,这段代码不起作用。

class UsersController < ApplicationController
      .
      .
      .
      def show
        @user = User.find(params[:id])
        @microposts = @user.microposts.where("something = 'true'")
        @titre = @user.nom
      end
    end

任何想法?

2 个答案:

答案 0 :(得分:4)

  def show
    @user = User.find(params[:id])
    @microposts = @user.microposts.where(something: true)
    @titre = @user.nom
 end

有关缩小查询范围的详细信息,请参阅herehere

答案 1 :(得分:2)

像这样写:

Micropost.where(user_id: params[:id], something: true)