# Rails and Scaffold question

**URL:** <https://www.ruby-forum.com/t/rails-and-scaffold-question/109362>\
**Category:** Rails\
**Created:** [July 13, 2007, 7:20pm UTC](https://www.ruby-forum.com/t/rails-and-scaffold-question/109362 "2007-07-13T19:20:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Onslauth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/f1d935/32.png) [@Onslauth](https://www.ruby-forum.com/u/Onslauth)\
**Post date:** [July 13, 2007, 7:20pm UTC](https://www.ruby-forum.com/t/rails-and-scaffold-question/109362/1 "2007-07-13T19:20:12Z")

</div>

Hi.

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:  
belongs\_to :my\_privilege

And in models/my\_privilege.rb I added:  
has\_many :my\_users

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  
belongs\_to :privilege  
And the following to models/privilege.rb  
has\_many :users

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.

Regards.  
/Cole

---

<div class="post-metadata">

**Author:** ![Onslauth](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/f1d935/32.png) [@Onslauth](https://www.ruby-forum.com/u/Onslauth)\
**Post date:** [July 14, 2007, 6:18pm UTC](https://www.ruby-forum.com/t/rails-and-scaffold-question/109362/2 "2007-07-14T18:18:43Z")

</div>

Try doing your scaffold commands using the actual ruby model name rather  
than the table name e.g.

ruby script/generate scaffold MyUser  
ruby script/generate scaffold MyPrivilage

and just incase also check your user has a matching privilage 😉

Cheers  
Luke
