I hope it’s ok that I ask this question here, I don’t wanted to
subscribe to two other lists. If I have more questions regarding ruby
itself I will do this.
I’ve seen the ||= operator several times in codes, screencasts and so
on, but I don’t know it’s use. Surely this will be no problem for
nearly every developer here, so thank you in advance.
Here it is in context
@get_latest ||= Posts.find :all, :limit => 10, :order => “created_at
||= means “return what you have already. If you’re nil, then go get a
This is called “menoinizing” and is a nice way to cache values.
Rails actually uses this a lot… when you have Post has_many :comments,
the .comments method on an instance of a Post is created and does
@comments ||= Comment.find :all, :conditions => [“post_id = ?”, self.id]
This way if you call @post.comments from your controller and then again
your view, it only hits the db once.
Thank you both !
This completely explained my question.