Forum: Ruby on Rails Rails and Scaffold question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Onslauth (Guest)
on 2007-07-13 21:20
(Received via mailing list)
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
Luke P. (Guest)
on 2007-07-14 20:18
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
This topic is locked and can not be replied to.