Optionally pass a parameter?

P

Peter Alvin

In general, how do you optionally pass an optional qualifier to a Ruby
def?

For example, for some model in Rails, like this:

MyDBClass.find:)all, ...

How do I optionally add a condition? This generates a syntax error:

some_test ? :conditions => "id=100" : nil,

I guess it doesn't like that I only want to add the :conditions
occasionally. What is the Ruby best practice or idiom for this?

Pete
 
J

James Coglan

[Note: parts of this message were removed to make it a legal post.]
some_test ? :conditions => "id=100" : nil


Try turning it inside out:

:conditions => (some_test ? "id=100" : nil)

or:

:conditions => (some_test && "id=100")

Not too sure about what the second one will cause Rails to do if some_test
is false rather than nil, but worth a try.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top