Forum: Ruby on Rails Scaffold with Foreign Key

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.
Vincent G. (Guest)
on 2006-03-15 08:54
(Received via mailing list)
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.
Craig W. (Guest)
on 2006-03-15 09:04
(Received via mailing list)
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
Vincent G. (Guest)
on 2006-03-15 09:13
(Received via mailing list)
Thanks.  Just tried that.. It didn't work though.
Craig W. (Guest)
on 2006-03-15 09:31
(Received via mailing list)
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)

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

Craig
joey__ (Guest)
on 2006-03-15 09:51
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] http://en.wikipedia.org/wiki/Scaffolding
Vincent G. (Guest)
on 2006-03-15 20:07
(Received via mailing list)
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.
Craig W. (Guest)
on 2006-03-16 00:05
(Received via mailing list)
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
Vincent G. (Guest)
on 2006-03-16 00:54
(Received via mailing list)
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.
Jeremy E. (Guest)
on 2006-03-16 02:56
(Received via mailing list)
On 3/15/06, Vincent G. <removed_email_address@domain.invalid> 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:
http://wiki.rubyonrails.com/rails/pages/Scaffoldin....
Vincent G. (Guest)
on 2006-03-16 06:47
(Received via mailing list)
looks like what i need.  Whats the difference between edge rails and
regular
rails?
Jeremy E. (Guest)
on 2006-03-16 21:15
(Received via mailing list)
On 3/15/06, Vincent G. <removed_email_address@domain.invalid> wrote:
> looks like what i need.  Whats the difference between edge rails and regular
> rails?

http://wiki.rubyonrails.com/rails/pages/EdgeRails
This topic is locked and can not be replied to.