Wednesday, October 7, 2009

[Rails] Re: Return "title" column if "caption" column is empty?

> On Wed, Oct 7, 2009 at 11:11 AM, Joshua Muheim
> <rails-mailing-list@andreas-s.net> wrote:
>> class Page < ActiveRecord::Base
>> def caption
>> caption ? caption : title
>> end
>> end
>
> This will produce an infinite loop. I think you want something more
> like
>
> self.caption ? self.caption : self.title
>
> But honestly I'd rename the method to something else for clarity.

Agreed. At some point you're going to want to access 'caption'
directly and the above won't let you do that.

Plus your team might not realize you're adding magic.

I've always gone with "preferred"... that is..

def preferred_caption
# code to get me the ideal caption for the fields available
end

That helps remind me that I'm not necessarily getting back the caption
itself, but the ideal caption for the given situation.

It's worked well for me so far.

-philip

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate