Wednesday, October 7, 2009

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

Use read_attribute(:caption).

2009/10/7, Joshua Muheim <rails-mailing-list@andreas-s.net>:
>
> Hi all
>
> Oh my god, this really shouldn't be a tough one, but I just don't get
> it. ;-)
>
> class Page < ActiveRecord::Base
> def caption
> caption ? caption : title
> end
> end
>
> I want to return the content of the title column when the caption column
> is empty:
>
> p.caption # Caption is not empty
> # => "caption"
>
> p.caption # Caption is empty
> # => "title"
>
> Not astonishing, I get a StackLevelTooDeep Exception with my "solution"
> above... But how can I do it right?
>
> Thanks a lot... My brain is just a bit wrecked today...
> Josh
> --
> Posted via http://www.ruby-forum.com/.
>
> >
>

--
Von meinen Mobilgerät aus gesendet

--~--~---------~--~----~------------~-------~--~----~
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