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 に答える