Forum: Ruby on Rails Inserting the parent Id to the child table

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
5190273ecab4b93b2d4e7529dcc1f7c6?d=identicon&s=25 unknown (Guest)
on 2006-01-19 20:22
(Received via mailing list)
The other day, I was looking at the way to insert into the child record.
I looked at the api's at the following link.

I am not very clear still the way to do it.

I would like to give you the background once again.

I have say two tables.
Table A : students
Table B : addresses

Table B has student_id as the foreign key to Table A

 If I am displaying the list of students and wanted to get individual
 students addresses based on their id, is the only option is to get the

 id from students controller method list into the session and use that
 session value to populate the student_id in the addresses table?

 List.rhtml (students)
  <td><%= link_to("Address" , :controller => "addresses", :action =>
 "list", :id => student) %>

 def list
     @addresses = Address.find(:all, :conditions => [ "student_id=
 (?)", params[:id]], :limit => 5)
     @session["student_id"] = params[:id] End

 def create
     @addresses =[:addresses])

I was told that this is not the right way, instead think in terms of the
models something like this:

class Student < ActiveRecord::Base
has_many :addresses

class Address < ActiveRecord::Base
belongs_to :student

@student = Student.find(params[:id])
@addrs = @student.addresses
@address =[:address]) # and so on...

I am not very clear even after reading the api. Could someone give me an
example of how this is can be achieved using ruby.

Silvy Mathews
This topic is locked and can not be replied to.