0

Let's say I wanna do

SomeModel.where(blahblah)

and if certain param has been set, then I also want to order the selection

SomeModel.where(blahblah).order(blahblah)

I don't want to have to do

if something
   SomeModel.where(blahblah)
else
   SomeModel.where(blahblah).order(blahblah)
end

because it's repetitive.

Is there some way that allows me to construct a SQL statement with conditionals, and then execute it later? Something similar to constructing a SQL string through concatenation and then executing the string?

1 Answer 1

1

Yes, that's the point of ARel:

x = SomeModel.where(blahblah)
x = x.order(blah) if something
Sign up to request clarification or add additional context in comments.

1 Comment

You're using Rails. You're already doing .where and .order. That's all ARel. ActiveRecord::Relation. In other words, you don't need to add another gem, you already have the gem you need for this.

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.