Scaffold problem

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!

On 2/13/07, cyril [email protected] 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.

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.

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

…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 :slight_smile:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs