# Scaffold problem

**URL:** <https://www.ruby-forum.com/t/scaffold-problem/85502>\
**Category:** Rails\
**Created:** [February 14, 2007, 2:02am UTC](https://www.ruby-forum.com/t/scaffold-problem/85502 "2007-02-14T02:02:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cyril](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/35a633/32.png) [@cyril](https://www.ruby-forum.com/u/cyril)\
**Post date:** [February 14, 2007, 2:02am UTC](https://www.ruby-forum.com/t/scaffold-problem/85502/1 "2007-02-14T02:02:26Z")

</div>

Hi everybody,

I’ve got a true problem with RoR. My DB looks like:

abcs (1,_) \<-- (1,1) abcs\_xyzs (1,1) --\> (1,_) xyzs

For macking a wiki RoR scaffold, I enter this:

# ruby script/generate scaffold abc

# ruby script/generate scaffold xyz

But:

who could we explain to RoR that there is a other table witch is  
linking abcs and xyzs ? (by the more simple way if possible :))

Thanks you!

---

<div class="post-metadata">

**Author:** ![cyril](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/35a633/32.png) [@cyril](https://www.ruby-forum.com/u/cyril)\
**Post date:** [February 14, 2007, 2:06am UTC](https://www.ruby-forum.com/t/scaffold-problem/85502/2 "2007-02-14T02:06:19Z")

</div>

On 2/13/07, cyril [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> # ruby script/generate scaffold abc
> 
> # ruby script/generate scaffold xyz
> 
> But:
> 
> who could we explain to RoR that there is a other table witch is  
> linking abcs and xyzs ? (by the more simple way if possible :))
> 
> Thanks you!

In your abc model:

has\_and\_belongs\_to\_many :xyz

In your xyx model:

has\_and\_belongs\_to\_many :abc

Is this what you were asking?

–  
Andrew S.

---

<div class="post-metadata">

**Author:** ![cyril](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/35a633/32.png) [@cyril](https://www.ruby-forum.com/u/cyril)\
**Post date:** [February 14, 2007, 5:46am UTC](https://www.ruby-forum.com/t/scaffold-problem/85502/3 "2007-02-14T05:46:34Z")

</div>

I think he is asking if it is possible to get the scaffolding to reflect  
the relationships, so that you can assign abc’s to xyz’s as you create  
or edit them.

---

<div class="post-metadata">

**Author:** ![cyril](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/35a633/32.png) [@cyril](https://www.ruby-forum.com/u/cyril)\
**Post date:** [February 14, 2007, 12:05pm UTC](https://www.ruby-forum.com/t/scaffold-problem/85502/4 "2007-02-14T12:05:22Z")

</div>

Thx for your answers.

In fact:

1. I don’t understand why we must add something  
like that “has\_and\_belongs\_to\_many” because  
of I respect the Rails conventions. Normally,  
I think Rails would automaticly understand  
where are the relation.

2. I was try but it isn’t working…

For example, if I have this other schema (more simple):

users (1,1) → (0,\*) homes

I know that we must add…  
 → in User model “belongs\_to :home”  
 → in Home model “has\_many :users”

So, because of in my users MySQL table there is “home\_id”, Rails could  
find alone the way of the relation… 'cause it know “home\_id” is the  
primary key of homes table.

If you need, I copy/past a dump of my test DB:

– Begin --------------------------------------------------

CREATE TABLE `homes` (  
`id` int(11) NOT NULL auto\_increment,  
`name` varchar(32) NOT NULL,  
PRIMARY KEY (`id`),  
UNIQUE KEY `name` (`name`)  
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO\_INCREMENT=2 ;

INSERT INTO `homes` (`id`, `name`) VALUES (1, ‘Blue home’);

CREATE TABLE `users` (  
`id` int(11) NOT NULL auto\_increment,  
`name` varchar(32) NOT NULL,  
`home_id` int(11) NOT NULL,  
PRIMARY KEY (`id`),  
UNIQUE KEY `name` (`name`)  
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO\_INCREMENT=2 ;

INSERT INTO `users` (`id`, `name`, `home_id`) VALUES (1, ‘Jo’, 1);

– End ----------------------------------------------------

And here, I copy/past all I do:

– Begin --------------------------------------------------

home:~/www/rails cyril$ rails test  
create  
create app/controllers  
create app/helpers  
create app/models  
create app/views/layouts  
create config/environments  
create components  
create db  
create doc  
create lib  
create lib/tasks  
create log  
create public/images  
create public/javascripts  
create public/stylesheets  
create script/performance  
create script/process  
create test/fixtures  
create test/functional  
create test/integration  
create test/mocks/development  
create test/mocks/test  
create test/unit  
create vendor  
create vendor/plugins  
create tmp/sessions  
create tmp/sockets  
create tmp/cache  
create Rakefile  
create README  
create app/controllers/application.rb  
create app/helpers/application\_helper.rb  
create test/test\_helper.rb  
create config/database.yml  
create config/routes.rb  
create public/.htaccess  
create config/boot.rb  
create config/environment.rb  
create config/environments/production.rb  
create config/environments/development.rb  
create config/environments/test.rb  
create script/about  
create script/breakpointer  
create script/console  
create script/destroy  
create script/generate  
create script/performance/benchmarker  
create script/performance/profiler  
create script/process/reaper  
create script/process/spawner  
create script/runner  
create script/server  
create script/plugin  
create public/dispatch.rb  
create public/dispatch.cgi  
create public/dispatch.fcgi  
create public/404.html  
create public/500.html  
create public/index.html  
create public/favicon.ico  
create public/robots.txt  
create public/images/rails.png  
create public/javascripts/prototype.js  
create public/javascripts/effects.js  
create public/javascripts/dragdrop.js  
create public/javascripts/controls.js  
create public/javascripts/application.js  
create doc/README\_FOR\_APP  
create log/server.log  
create log/production.log  
create log/development.log  
create log/test.log  
home:~/www/rails cyril$ cd test/  
home:~/www/rails/test cyril$ vi config/database.yml  
home:~/www/rails/test cyril$ ruby script/generate scaffold home  
exists app/controllers/  
exists app/helpers/  
create app/views/homes  
exists test/functional/  
dependency model  
exists app/models/  
exists test/unit/  
exists test/fixtures/  
create app/models/home.rb  
create test/unit/home\_test.rb  
create test/fixtures/homes.yml  
create app/views/homes/\_form.rhtml  
create app/views/homes/list.rhtml  
create app/views/homes/show.rhtml  
create app/views/homes/new.rhtml  
create app/views/homes/edit.rhtml  
create app/controllers/homes\_controller.rb  
create test/functional/homes\_controller\_test.rb  
create app/helpers/homes\_helper.rb  
create app/views/layouts/homes.rhtml  
create public/stylesheets/scaffold.css  
home:~/www/rails/test cyril$ ruby script/generate scaffold user  
exists app/controllers/  
exists app/helpers/  
create app/views/users  
exists test/functional/  
dependency model  
exists app/models/  
exists test/unit/  
exists test/fixtures/  
create app/models/user.rb  
create test/unit/user\_test.rb  
create test/fixtures/users.yml  
create app/views/users/\_form.rhtml  
create app/views/users/list.rhtml  
create app/views/users/show.rhtml  
create app/views/users/new.rhtml  
create app/views/users/edit.rhtml  
create app/controllers/users\_controller.rb  
create test/functional/users\_controller\_test.rb  
create app/helpers/users\_helper.rb  
create app/views/layouts/users.rhtml  
identical public/stylesheets/scaffold.css  
home:~/www/rails/test cyril$ vi app/models/home.rb  
home:~/www/rails/test cyril$ vi app/models/user.rb  
home:~/www/rails/test cyril$ ruby script/server

– End ----------------------------------------------------

My problem is that, when I want to add a user, I enter the URL:  
[http://127.0.0.1:3000/users/new](http://127.0.0.1:3000/users/new)

…and the body page print this form (without the home\_id input of my  
users table, witch if the primarykey of homes table… So I think RoR  
don’t understood very well the MySQL DB):

Name

After I submit this form (with for example “Mike” name), I can see in my  
MySQL DB there is a new user (so it’s good) but with a home\_id = 0 (so  
it’s don’t working).

And as you can see in this draw:

----- homes table -----  
id name  
1 Blue home  
----- users table -----

id name home\_id  
1 Jo 1  
2 Mike 0 ← my problem is here 🙂

Sorry for this to long message.  
Thanks you if you know why it’s don’t working very well.
