Matthew Palmer wrote:
Off the top of my head:
class Student
has_one :state, :foreign_key => 'state'
end
These feedbacks follow:
The "has_one ..." should be "has_many.." and It should
be in "class State..", i.e., One-State maps-to-many Student, the
opposite of what you suggested.
The "belongs_to ..." should be in "class Student ...".
So, the above becomes:
class Student < ActiveRecord::Base
belongs_to :state
end
class State
belongs_to :student, :foreign_key => 'state'
end
This should become:
class State < ActiveRecord::Base
has_many :student
end
Not only that, we also need to modify attribute
"state" in table "students" because
it is "CHAR"; it should be "INT". We also add
a foreign key in table "students" like so,
constraint fk_items_state foreign key (state) references states(id).
So then you can:
s = Student.find(4)
puts s.state.city
And only then the above will work.
Apparently, there are other methods, that
are more verbose than the above, but that's
for next time.
No wonder, it's extremely painful to port
my legacy perl applications and others to RoR.
Involves extensive re-engineering the database table
definitions, for a start.
My experience so far is, better to start from
scratch. I, of course, don't know what're the experiences of
others.
If you rename the 'state' field in students to 'state_id', you can drop the
:foreign_key bits of both above.
Tried this. Did not work.
BTW, your State table is terribly named. 'localities' or something might be
more appropriate.
What's in a name ?
Anyways, thanks.
O Plameras
_______________________________________________
coders mailing list
[email protected]
http://lists.slug.org.au/listinfo/coders