This is great for writing short and poignant if-statements in Ruby and IMO makes code a bit more readable.
The syntax of the ternary operator:
[condition] ? [true expression] : [false expression]
[sourcecode language=’ruby’]irb(main):182:0> animal = “Dog”
irb(main):183:0> animal == “Dog” ? “You are a Dog” : “WHAT ARE YOU?!?”
=> “You are a Dog”[/sourcecode]
In this exercise I had Posts with many image attachments attached with attachment_fu and rmagick. I also had a bunch of old data that i wanted to import in using a rake task and finally all image attachments needed to be watermarked.
First my assumptions:
- “A Post can have many Image attachments”
- You have the attachment_fu plugin installed already
Here is how it’s done.
This blog is designed to function as a sort of journal where i can document code snippets and stuff like that. If you have anything to say, don’t hold back!