I am at a loss why this is not working.....I just want to show the
company name if the contact instead of the .id. Any assistance would be
grateful.
class Contact < ActiveRecord::Base
belongs_to :company
class ContactsController < ApplicationController
def index
@contacts = Contact.all
@company = Company.find(@contact.company_id)
and in the view:
<%= @company.company_name%>
--
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
On Sat, Jul 21, 2012 at 4:31 PM, James Harris <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I am at a loss why this is not working.....I''m at a loss as to why you think it should... :-)> class ContactsController < ApplicationController > def index > @contacts = Contact.all > > @company = Company.find(@contact.company_id)What would you expect here? @contact above is not defined in the code you''re showing us. Are you trying to display the company name of one contact or all? What is the association between Contact and Company? -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Ahh. this works ok in my show view because it looks for only one record for the id. How do I show the correct company name for each contact row in my list table of all contacts which have a belongs_to :company association. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Sat, Jul 21, 2012 at 5:11 PM, James Harris <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Ahh. this works ok in my show view because it looks for only one record > for the id. How do I show the correct company name for each contact row > in my list table of all contacts which have a belongs_to :company > association.If your associations are correct, you should be able to use e.g. `contact.company.company_name` . Open up a console and try it. -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
In the console doing contact = Contact.find(1) finds my contact and the contact.company.company_name returned the correct company name. Having contact.company.company_name in the index.html file returns an error: undefined method `company_name'' for nil:NilClass -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Jul 21, 2012, at 8:52 PM, James Harris wrote:> In the console doing contact = Contact.find(1) finds my contact and the > contact.company.company_name returned the correct company name. > > Having contact.company.company_name in the index.html file returns an > error: > > undefined method `company_name'' for nil:NilClassYou can figure out why that is nil, or you can just guard around it: contact.company.try(:company_name) My guess is that this particular contact does not have a company associated with it. Walter> > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit https://groups.google.com/groups/opt_out. > >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Sat, Jul 21, 2012 at 5:52 PM, James Harris <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> In the console doing contact = Contact.find(1) finds my contact and the > contact.company.company_name returned the correct company name. > > Having contact.company.company_name in the index.html file returns an > error: > > undefined method `company_name'' for nil:NilClassUnstated assumption: in your "index" page you''re doing something like <% @contacts.each |contact| do %> <%= contact.company.company_name %> <% end %> If not, no, of course not, "contact" isn''t defined. Cargo-culting example code into your project isn''t going to help; you need to think about what it''s intended to do. -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Using Walters solution has worked. contact.company.try(:company_name) So my understanding of this now is that because some of the contact records have no company defined (a nil object or NilClass). By using .try it responds nil if no company is found. So does that mean that if I have a field that uses a foreign key but is not required I should use eg. try(*a, &b)? Or is there another solution? Thank you. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Sat, Jul 21, 2012 at 7:44 PM, James Harris <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Using Walters solution has worked. contact.company.try(:company_name) > > So my understanding of this now is that because some of the contact > records have no company defined (a nil object or NilClass). By using > .try it responds nil if no company is found. > > So does that mean that if I have a field that uses a foreign key but is > not required I should use eg. try(*a, &b)? Or is there another solution?First, is it OK that a contact has no company, or is that a data error that should have been caught by validations? If it''s OK to have a nil company association, then the question is: how many of these potential error-generating statements do you have? If it''s only one or two, you can use the `try` approach above or 1) Use a rescue: contact.company.company_name rescue '''' 2) Test for the existence of company: if contact.company contact.company.company_name end 3) Write a helper to return empty values for nil companies: # untested example follows :-) company_info_for(contact.company, '''') # replaces contact.company.company_name in view def company_info_for(company, attr) defaults = { :company_name => '''' } return defaults''attr] if company.nil? company.send(attr) end -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Apologies, fat-fingered premature send ... On Sun, Jul 22, 2012 at 9:07 AM, Hassan Schroeder <hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 3) Write a helper to return empty values for nil companies: > # untested example follows :-)company_info_for(contact.company, ''company_name'') # replaces contact.company.company_name in view def company_info_for(company, attr) defaults = { :company_name => '''' } return defaults[attr] if company.nil? company.send(attr) end HTH! -- Hassan Schroeder ------------------------ hassan.schroeder-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org http://about.me/hassanschroeder twitter: @hassan -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.