Alrighty folks, I’m really trying here, and want to succeed with Ruby on
Rails.
I’m trying to display data that is linked from two tables using the
Active Record naming conventions, as described in Agile Web book.
The stripped-down basics;
Database
drop table if exists people;
CREATE TABLE people
(
id
int(6) NOT NULL auto_increment,
lastname
varchar(30) NOT NULL,
personkind_id
int(6) NOT NULL default ‘23’,
PRIMARY KEY (id
)
)
drop table if exists person_kinds;
CREATE TABLE person_kinds
(
id
int(6) NOT NULL auto_increment,
kind
varchar(30) NOT NULL,
PRIMARY KEY (id
)
)
###########################
Using ruby script/generate scaffold
Controllers;
class PersonAdminController < ApplicationController
def list
@person_pages, @people = paginate(:people, :order_by =>‘lastname’,
:per_page => 10)
@person_kinds = PersonKind.find_all
end
class PersonKindAdminController < ApplicationController
###########################
Models
class Person < ActiveRecord::Base
belongs_to :person_kind
end
class PersonKind < ActiveRecord::Base
has_many :people
end
###########################
list.rhtml
People by Field
Last Name |
Kind |
|
<%= link_to person.lastname, :action => "show", :id => person.id %> | <%= person.firstname %> | <%= person.person_kind.kind %> |
<%= link_to "Add New Person", :action => "new" %>
##############################
The <%= person.person_kind.kind %> line in the list.rhtml is what is
causing my brain to turn to tapioca.
I really thought that that would work. I know this is very basic Rails
stuff, but I’m stuck. Any help would be greatly appreciated.
Kindest regards.