Noob Q: How to access a belongs_to object

Sorry for the simplicity on this.  I hope this makes for a quick response.

 

I have two classes:

 

class UserProfile < ActiveRecord::Base
  belongs_to :UserGroup
end

class UserGroup < ActiveRecord::Base

  has_many :UserProfiles

end

 

I want to display the UserGroup.Name for an UserProfile instance.

 

This snippet returns the correct User_Group id:

 

@user_profile.User_Group_id

 

This code seemed the most logical to me:

 

@user_profile.UserGroup.Name

 

but I get this error:

You have a nil object when you 
didn't expect it!
The error occurred while evaluating nil.Name
What 
should the call be?
Thanks in 
advance!


Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

FYI, I figured this out. I shouldn’t have been using capital letters
in my classes. But now, I’ve run into a different problem.

These two classes:
class UserProfile < ActiveRecord::Base
belongs_to :UserGroup
end

class UserGroup < ActiveRecord::Base
has_many :UserProfiles
end

become:
class UserProfile < ActiveRecord::Base
belongs_to :usergroup
end

class UserGroup < ActiveRecord::Base
has_many :userprofiles
end

I’m not getting the error below when I make this call:

<% for user_profile in @user_profiles %>

<%=h user_profile.usergroup.Name %>

========Error======
uninitialized constant UserProfile::Usergroup

Thoughts?

On Feb 15, 4:41 pm, stevepeters [email protected] wrote:

end

become:
class UserProfile < ActiveRecord::Base
belongs_to :usergroup

Should be ‘user_group’

end

class UserGroup < ActiveRecord::Base
has_many :userprofiles

and ‘user_profiles’

Multiple words in the class name correspond to multiple words in the
table and relationship names. Here’s another tip to preserve sanity:
Follow the dictionary when deciding whether a name is one word or
multiple words. This rule tells you to call the class ‘UserGroup’
and the table ‘user_groups’ instead of ‘Usergroup’ and ‘usergroups’.