Forum: Ruby on Rails Rails day 2: where is my association?

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.
Zac E. (Guest)
on 2006-01-25 21:53
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 %>
    <td><%=h package.send(column.name) %></td>
  <% end %>

this wonâ??t show the group_id

but  this will

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

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
Zac E. (Guest)
on 2006-01-25 22:51
> <% for column in Package.content_columns %>
>     <td><%=h package.send(column.name) %></td>
>   <% end %>
>
> this wonâ??t show the group_id

answering part of my own question..


http://railsmanual.org/class/ActiveRecord::Base/co...

content_columns()
Returns an array of column objects where the primary id, all columns
ending in "_id" or "_count", and columns used for single table
inheritance have been removed.

-zac
This topic is locked and can not be replied to.