0

I want to get all the microposts where 'something' = true.

This code works fine

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

But when I tried to make a where sql method this code doesn't work.

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

any idea ?

4

2 回答 2

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

有关缩小查询范围的更多信息,请参见此处此处

于 2012-08-10T14:46:36.330 回答
2

像这样写:

Micropost.where(user_id: params[:id], something: true)
于 2012-08-10T14:41:58.773 回答