On Dec 12, 2010, at 4:59 PM, Rodrigo Mendonça wrote:

Great!!

when user_id is null (and can be null in some cases)

you can do this

<%post.each do |p|%>
     <%=p.user.name unless p.user.nil?%>
<%end>

other way is

<%=p.user.name unless p.user.empty?%>

Or, you can push that into the model and avoid the extra logic in the view:

class Post
  def user_name
    self.user ? self.user.name : "(none)"
  end
end

<% @posts.each do |post| %>
  <%= post.user_name %>
<% end %>

You could name the method something like "author_string" or "by_line", too. The idea is to keep the view very clean. You might be able to simply `delegate :name, :to => :user` in your Post model, but that would have given you the same problem if the post.user_id referred to a non-existent User.

-Rob


2010/12/12 Colin Law <[email protected]>
2010/12/12 Rodrigo Mendonça <[email protected]>:
> Search in app/model for the file User.rb (in singular)
> This file exists??

For posterity that should be user.rb not User.rb of course

Colin

--
Rodrigo Mendonça
(62) 8567-3142

Rob Biedenharn          
[email protected]     http://AgileConsultingLLC.com/
[email protected]               http://GaslightSoftware.com/

--
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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to