1

I have the following code in a rails controller:

@users = User.where(["first_name = :first_name or
                    last_name = :last_name or
                    company = :company",
                    { first_name: term, last_name: term,
                    company: term }])

Term is term = params[:search]

I don't like that I'm repeating term

{ first_name: term, last_name: term,
                        company: term }

is there a DRYER way to accomplish this?

Thank you!

2 Answers 2

6

You can do it like this:

@users = User.where("first_name = :term OR
  last_name = :term OR company = :term", term: term)
Sign up to request clarification or add additional context in comments.

Comments

2

You can use the hash syntax for setting the parameters

users = User.where('first_name = :term OR 
                    last_name = :term OR
                    company = :term', 
  term: term)

Comments

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.