views:

39

answers:

2

I am looking up all Organizations with the url "http://", and updating their attributes to "".

My attempt:

Organization(:all).select { |o| o.url = "http://" ? o.update_attribute("url","")}

Which returns a compile error:

SyntaxError: compile error
 (irb):2: syntax error, unexpected '}'
   from (irb):2

Any ideas?

A: 

I'm by no means a ruby expert, but my first suspicion is that you're using an assignment operator (=) instead of an equality operator (==). A quick google search for "ruby irb conditional" appears to prove this.

And you probably got the down vote because you did not include compilation errors in your question. If my guess is wrong, I can't even help try to interpret the error message, because you didn't provide it.

UPDATE: based on the first comment to the answer, I believe my first suspicion to be wrong - a misinterpretation of the intent of the line of code. But, then, this is what happens when error messages are not availble.

UPDATE2: first comment not there... maybe it was deleted or maybe I started typing in the wrong place...

atk
Thanks @atk. I'm reading up on this. Tried the double equality operator. Same compiler error. I imagine it's in my syntax. I appreciate the help nonetheless.
Trip
+2  A: 

Try using update_all

Organization.update_all("url = ''", ["url =?",'http://'])
Shikher
Brilliant. I had no idea how to use conditionals like that before. Thank you very very much.
Trip
What you were trying can be done in your way like this:Organization(:all).select{ |o| o.update_attribute("url","") if o.url == "http://"}The ternary operator you were using (the '?') was expecting a ':' and a second condition after that. But it encountered a '}' so raised an error.
Shikher
@Shiker: Nice answer..
Bragboy