to paint the picture I currently have a system which lists uses and the Department the user is assigned to. So my models look something like this. ####User Model class User < ActiveRecord::Base belongs_to :department end ####Department Model class Department < ActiveRecord::Base belongs_to :organization has_many :users end A user may not necessarily have a department which is fine however, when this user does not have a department this department value will be nil. When I go to print out this department value I get the following error You have a nil object when you didn''t expect it! The error occured while evaluating nil.name the name value is my department name. What''s the best way to tell rails to print out nothing if this object is set to nil? I was thinking of setting up a helper method. But I''m not sure that that is the best thing to do. Thanks in advance for any help. -- Posted via http://www.ruby-forum.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-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Using a method in your User model in place of accessing the relationship''s attributes is probably the best way to handle this. ####User Model class User < ActiveRecord::Base belongs_to :department def department_name department.nil? ? '''' : department.name end end Whenever you need the department name use that method user.department_name. Extending this a bit, you could open it up for all fields (renamed to prevent a conflict with the real attr_accessor for department): def department_attribute(field) department.nil? ? '''' : department.send(field) end Then you can do user.department_attribute(:name). Or, you could turn off whiny nils (see config/environments/development.rb), at the expense of obscuring bugs. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Eden Li wrote:> Using a method in your User model in place of accessing the > relationship''s attributes is probably the best way to handle this. > > ####User Model > class User < ActiveRecord::Base > belongs_to :department > > def department_name > department.nil? ? '''' : department.name > end > end > > Whenever you need the department name use that method > user.department_name. Extending this a bit, you could open it up for > all fields (renamed to prevent a conflict with the real attr_accessor > for department): > > def department_attribute(field) > department.nil? ? '''' : department.send(field) > end > > Then you can do user.department_attribute(:name). > > Or, you could turn off whiny nils (see > config/environments/development.rb), at the expense of obscuring bugs.That''s perfect! I ended up defining a method in the user model. This will help me with a number of other issues I was facing. I did not fully understand implementing methods in our model before. I was just used to declaring them for my controllers. Thank you so much for your help! -- Posted via http://www.ruby-forum.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-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
On Oct 14, 9:20 am, Stewart <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Eden Li wrote: > > Using a method in your User model in place of accessing the > > relationship''s attributes is probably the best way to handle this. > > > ####User Model > > class User < ActiveRecord::Base > > belongs_to :department > > > def department_name > > department.nil? ? '''' : department.name > > end > > end > > > Whenever you need the department name use that method > > user.department_name. Extending this a bit, you could open it up for > > all fields (renamed to prevent a conflict with the real attr_accessor > > for department): > > > def department_attribute(field) > > department.nil? ? '''' : department.send(field) > > end > > > Then you can do user.department_attribute(:name). > > > Or, you could turn off whiny nils (see > > config/environments/development.rb), at the expense of obscuring bugs.That''s perfect! I ended up defining a method in the user model. This > will help me with a number of other issues I was facing. I did not > fully understand implementing methods in our model before. I was just > used to declaring them for my controllers. Thank you so much for your > help! > > -- > Posted viahttp://www.ruby-forum.com/.You could also do stuff like this... @department.department_name rescue "None" or override the reader for the attribute... class User def department_name self[:department_name] || "None" end end --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---