[Rails] Rails day 2: where is my association?

zac elston
2006-01-25T19:53:56Z

I?m following along a few tutorials on the web and trying to implement
my own example, but I must be missing something because I can?t get
has_many or belongs_to to work like I expected.  now I have watched all
the videos and made a cookbook several times with different interfaces.
what I''m looking for is *not* a code snippet to solve something
(because
I already have that), I''m looking for a deeper understanding of
what''s
going on.  most of the examples I see on the net mix functionality with
UI so it''s difficult to see what part is a "here we get data"
and what
part is "here we format data"

anyway...

Here?s the setup:

CREATE DATABASE `rpms_development` ;
use rpms_development;

CREATE TABLE  `packages` (
 `id` INT( 255 ) NOT NULL AUTO_INCREMENT ,
 `name` VARCHAR( 255 ) NOT NULL ,
 `version` VARCHAR( 255 ) NOT NULL ,
 `group_id` INT( 255 ) NOT NULL ,
 `notes` TEXT NOT NULL ,
PRIMARY KEY (  `id` )
) TYPE = innodb;

CREATE TABLE  `groups` (
 `id` INT NOT NULL AUTO_INCREMENT ,
 `name` VARCHAR( 255 ) NOT NULL ,
PRIMARY KEY (  `id` )
) TYPE = innodb;

INSERT INTO groups (name) values ("Development");
INSERT INTO groups (name) values ("Test");
INSERT INTO groups (name) values ("Live");

INSERT INTO packages (name, version, group_id, notes) values
("packageA", "1", "1", "dev package 1");
INSERT INTO packages (name, version, group_id, notes) values
("packageA", "1", "2", "test package
1");
INSERT INTO packages (name, version, group_id, notes) values
("packageA", "1", "3", "live package
1");

rails rpms
cd rpms
script/generate scaffold group
script/generate scaffold package
script/server&

http://localhost:3000/packages

notice there is no group_id column.  Why?

Ok so moving on to the association, docs say I need to associate groups
and packages

CREATE TABLE  `groups_packages` (
 `id` INT( 255 ) NOT NULL AUTO_INCREMENT ,
 `group_id` INT( 255 ) NOT NULL ,
 `package_id` INT( 255 ) NOT NULL ,
PRIMARY KEY (  `id` )
) TYPE = innodb;

class Group < ActiveRecord::Base
has_many :packages
end
class Package < ActiveRecord::Base
belongs_to :group
end


now the default scaffold list.rhtml has

<% for column in Package.content_columns %>
<%=h package.send(column.name) %>
  <% end %>

this won?t show the group_id

but  this will

<% @packages.each do |package| %>
<%= link_to package.name, :action => "show", :id
=> package.id
%>
<%= package.group.name %>
<%= package.name %>

Questions:
what bit of insight am I missing here to understand why the group_id
column won?t showup under a for loop

and

after making an association, what frameworks exist to use that
association in the scaffold? I thought the point of the rails framework
was to see that I had a column and do something with it?

thanks

-zaq






--
Posted via http://www.ruby-forum.com/.