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.
81d253d1ae5907d4daa79ae9a4338d1b?d=identicon&s=25 Vincent Gov (Guest)
on 2006-03-15 07: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.
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-03-15 08:04
(Received via mailing list)
On Tue, 2006-03-14 at 22:54 -0800, Vincent Gov 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
81d253d1ae5907d4daa79ae9a4338d1b?d=identicon&s=25 Vincent Gov (Guest)
on 2006-03-15 08:13
(Received via mailing list)
Thanks.  Just tried that.. It didn't work though.
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-03-15 08:31
(Received via mailing list)
On Tue, 2006-03-14 at 23:10 -0800, Vincent Gov 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
6dab365a82517fb694650a57ee88e4a4?d=identicon&s=25 joey__ (Guest)
on 2006-03-15 08:51
Vincent Gov 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
81d253d1ae5907d4daa79ae9a4338d1b?d=identicon&s=25 Vincent Gov (Guest)
on 2006-03-15 19: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.
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-03-15 23: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
81d253d1ae5907d4daa79ae9a4338d1b?d=identicon&s=25 Vincent Gov (Guest)
on 2006-03-15 23: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.
58c44a4a506d878f9a112f1d7b7cb87e?d=identicon&s=25 Jeremy Evans (Guest)
on 2006-03-16 01:56
(Received via mailing list)
On 3/15/06, Vincent Gov <vincehgov@gmail.com> 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....
81d253d1ae5907d4daa79ae9a4338d1b?d=identicon&s=25 Vincent Gov (Guest)
on 2006-03-16 05:47
(Received via mailing list)
looks like what i need.  Whats the difference between edge rails and
regular
rails?
58c44a4a506d878f9a112f1d7b7cb87e?d=identicon&s=25 Jeremy Evans (Guest)
on 2006-03-16 20:15
(Received via mailing list)
On 3/15/06, Vincent Gov <vincehgov@gmail.com> 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.