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 ?

1
  • The problem may be that something is a boolean and not a string. Commented Aug 10, 2012 at 14:45

2 Answers 2

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

See here or here for more info on narrowing the query scope.

Sign up to request clarification or add additional context in comments.

1 Comment

@user1560922 - if the problem is solved, please accept the answer. Thanks
2

Write like this:

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

3 Comments

yes but on my users model: "has_many :microposts" will be useless then ?
What is the difference? Maybe it will be needed elsewhere.
Changing to something: true is a good suggestion, but there is no reason not to use @user.microposts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.