Scaffold with Foreign Key

I have two tables:

drop table if exists users;
create table users (
id int not null auto_increment,
username varchar(100) not null,
password varchar(20) not null,
firstname varchar(20) not null,
lastname varchar(20) not null,
primary key (id)
) TYPE = InnoDB;

drop table if exists listings;
create table listings (
id int not null auto_increment,
user_id int not null,
description text not null,
constraint fk_listings_user foreign key (user_id) references users(id),
primary key (id)
) TYPE = InnoDB;

and in my User model i have the line:
has_many :listing

and in my Listing model i have the line:
belongs_to :user

I generate my scaffold:
ruby script\generate Scaffold User Useradmin
ruby script\generate Scaffod Listing Listingadmin

the problem I’m having is that the scaffold view that is generated for
Listing does not have an entry for selecting a User. In fact, the
scaffold
skips the user_id column completely.

On Tue, 2006-03-14 at 22:54 -0800, Vincent G. wrote:

) TYPE = InnoDB;

the problem I’m having is that the scaffold view that is generated for
Listing does not have an entry for selecting a User. In fact, the
scaffold skips the user_id column completely.


shouldn’t user model be…

has_many :listings

Craig

Thanks. Just tried that… It didn’t work though.

On Tue, 2006-03-14 at 23:10 -0800, Vincent G. wrote:

    > password        varchar(20)     not null, 
    > constraint fk_listings_user foreign key (user_id)
    >
    ----
    shouldn't user model be...
    
    has_many :listings
    
    Craig

to be honest…I’ve always used pop-up selectors or auto_complete
selectors for this phase because the id is just some sequential number
that is not meaningful at all anyway. I’m not sure about the scaffold
methodology and don’t rely upon it getting everything done for me beyond
the specific table I am scaffolding.

Thus, in app/views/listings # and I’m too used to Linux to turn my /
into \

I would have a selector like… (watch email line wraps)

User
<%= options = [['Select a User', '']] + @user.sort{|a,b| a.name <=> b.name }.collect { |u| [u.name, u.id] } select 'listing', 'user_id', options %>

Craig

Vincent G. wrote:

I have two tables:

drop table if exists users;
create table users (
id int not null auto_increment,
username varchar(100) not null,
password varchar(20) not null,
firstname varchar(20) not null,
lastname varchar(20) not null,
primary key (id)
) TYPE = InnoDB;

drop table if exists listings;
create table listings (
id int not null auto_increment,
user_id int not null,
description text not null,
constraint fk_listings_user foreign key (user_id) references users(id),
primary key (id)
) TYPE = InnoDB;

and in my User model i have the line:
has_many :listing

and in my Listing model i have the line:
belongs_to :user

I generate my scaffold:
ruby script\generate Scaffold User Useradmin
ruby script\generate Scaffod Listing Listingadmin

the problem I’m having is that the scaffold view that is generated for
Listing does not have an entry for selecting a User. In fact, the
scaffold
skips the user_id column completely.
Scaffolding is a temporary framework used to support people and material
in the construction or repair of buildings and other large
structures.[1]

As you can from the aboce citation, scaffolding is a temporary structure
as you build out the rest of your application. It does not support any
typ of association.

Joey
http://www.feedreed.com

[1] Scaffolding - Wikipedia

I think I provided the code to do that last night before I went to bed
but as ‘joey’ says…I haven’t seen scaffold do that sort of
thing…perhaps in the future…

Craig

Thanks Craig, I’ll try it. I guess I was just surprised to run into
this
issue. Rails seemed to take care of every administrative task up until
this
one.

I understand the concept. I just thought that Rails scaffolding would
recognize the column and create a pull down menu for me. The work isnt
really hard to do, but since this column is a constraint in my table, it
would be a lot more efficient if rails had made a generic form object
for
it. If Rails is incapable of that and the problem with foreign keys is
a
general case for Rails scaffolding, then I will proceed to add my own
form
objects.

looks like what i need. Whats the difference between edge rails and
regular
rails?

On 3/15/06, Vincent G. [email protected] wrote:

I understand the concept. I just thought that Rails scaffolding would
recognize the column and create a pull down menu for me. The work isnt
really hard to do, but since this column is a constraint in my table, it
would be a lot more efficient if rails had made a generic form object for
it. If Rails is incapable of that and the problem with foreign keys is a
general case for Rails scaffolding, then I will proceed to add my own form
objects.

Take a look at the Scaffolding Extensions plugin:
Peak Obsession.

On 3/15/06, Vincent G. [email protected] wrote:

looks like what i need. Whats the difference between edge rails and regular
rails?

http://wiki.rubyonrails.com/rails/pages/EdgeRails