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!
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 %>
...
<td><%=h user_profile.usergroup.Name %></td>
...
========Error======
uninitialized constant UserProfile::Usergroup
Thoughts?
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
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'.