If logic for Polymorphic data

0 views
Skip to first unread message

Finding_Zion

unread,
Jan 21, 2009, 9:44:15 AM1/21/09
to ChicagoRuby.org
In my polymorphic model, some projects have a cover_image_name and
some don't. My first attempt failed and I was wondering why.

<% if (project.data.has_attribute? "cover_image_name" && !
project.data.cover_image_name.blank? ) %>
<%= image_tag "#{IMAGE_URL_THUMB}/#{project.data.cover_image_name}"
%>
<% end %>

I expected the second condition to be tested if the first one was
true. Below is what works.

<% if (project.data.has_attribute? "cover_image_name" ) %>
<%= image_tag "#{IMAGE_URL_THUMB}/#{project.data.cover_image_name}"
if !project.data.cover_image_name.blank? %>
<% end %>

Thanks for any clarifications,

Jeremy

kim chirnside

unread,
Jan 22, 2009, 1:04:50 PM1/22/09
to ChicagoRuby.org
I would suggest it is a gotcha with regard to operator precedence and
the ability to not parenthesise arguments in ruby. Basically, the
first case has executed like this:

if project.data.has_attribute?( "cover_image_name" && !
project.data.cover_image_name.blank? )

Without including the brackets to delimit the argument list for
has_attribute?, the "cover_image_name" has been applied as the left
argument to &&.

Put the brackets in and it should be fine:

if project.data.has_attribute?("cover_image_name") && !
project.data.cover_image_name.blank?

end

Cheers,
Kim

Finding_Zion

unread,
Jan 30, 2009, 9:17:26 AM1/30/09
to ChicagoRuby.org
Kim,

Thanks that fixed the problem. Leaving out parenthesis is sometimes
nice, but it bites has well.

Jeremy
Reply all
Reply to author
Forward
0 new messages