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 <clan...@googlemail.com>
2010/12/12 Rodrigo Mendonça <den...@gmail.com>:
> 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          
r...@agileconsultingllc.com     http://AgileConsultingLLC.com/
r...@gaslightsoftware.com               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 rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to