Ive run into a problem when trying to create scaffold code and using
database tables that have the underscore character in them. I have 2
tables, my_users and my_privileges. my_user has a foreign key that
links into my_privileges.
So what I did was generate scaffold code with the following lines:
ruby script/generate scaffold my_user
ruby script/generate scaffold my_privilege
In models/my_user.rb I added:
And in models/my_privilege.rb I added:
Then in the views/my_users/show.rhtml, I added the following line:
<%= @my_user.my_privilege.name %>
However, I then get the following error:
“You have a nil object when you didn’t expect it!
The error occurred when evaluating nil.name”
I then tried all this without the underscore in the table name, using
users and privileges as the tables names, and then generated the
scaffold code witht the following lines:
ruby script/generate scaffold user
ruby script/generate scaffold privilege
And then added the following to models/user.rb
And the following to models/privilege.rb
And then in views/users/show.rhtml, I have the following line:
<%= @user.privilege.name %>
And this works fine, and shows the name field in the privileges table.
So what I would like to know, is what is the correct way to generate
scaffold code that will be using a table with the underscore character
in it? Is there another way to do this or is there perhaps something
that I am doing wrong?
Any help or suggestions would be appreciated.