Silvy@funmail.com
2006-Jan-18 07:10 UTC
[Rails] Inserting the parent Id to the child table
Hi All, I need your suggestions for the following. I am still learning RoR and pardon my ignorance. 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) %> Addresses_controller ------------- def list @addresses = Address.find(:all, :conditions => [ "student_id= (?)", params[:id]], :limit => 5) @session["student_id"] = params[:id] End def create @addresses = Address.new(params[:addresses]) @addresses.student_id=@session["student_id"] .............. end +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Are there any other way to do this. Any help appreciated. Thanks in advance. Silvy Mathews
Joshua Susser
2006-Jan-18 07:36 UTC
[Rails] Re: Inserting the parent Id to the child table
It looks like you haven''t yet gotten the Rails way of doing this stuff. Don''t think so much about the db, but think more about the models. class Student < ActiveRecord::Base has_many :addresses ... end class Address < ActiveRecord::Base belongs_to :student ... end @student = Student.find(params[:id]) @addrs = @student.addresses @address = @addrs.build(params[:address]) # and so on... Check out the docs on ActiveRecord associations, specifically has_many. http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html unknown wrote:> Hi All, > I need your suggestions for the following. I am still learning RoR and > pardon my ignorance. > 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) %> > > Addresses_controller > ------------- > def list > @addresses = Address.find(:all, :conditions => [ "student_id= (?)", > params[:id]], :limit => 5) > @session["student_id"] = params[:id] > End > > def create > @addresses = Address.new(params[:addresses]) > @addresses.student_id=@session["student_id"] > .............. > end > +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > > Are there any other way to do this. Any help appreciated. > Thanks in advance. > > Silvy Mathews-- Posted via http://www.ruby-forum.com/.
Silvy@funmail.com
2006-Jan-18 07:55 UTC
[Rails] Re: Inserting the parent Id to the child table
Looks like that''s true. Could you give me some more hint. The methods you have written should go to which method in the controller. I am trying to show the list of addresses associated with the student when clicked from the student''s list. My question is, if I need to add a new address, how do I get that id (not when I am trying to edit a particular address of the associated student). If you can give me some more hints, that would be wonderful. I looked at the api''s. Thanks Silvy Mathews -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Joshua Susser Sent: Wednesday, January 18, 2006 12:13 AM To: rails@lists.rubyonrails.org Subject: [Rails] Re: Inserting the parent Id to the child table It looks like you haven''t yet gotten the Rails way of doing this stuff. Don''t think so much about the db, but think more about the models. class Student < ActiveRecord::Base has_many :addresses ... end class Address < ActiveRecord::Base belongs_to :student ... end @student = Student.find(params[:id]) @addrs = @student.addresses @address = @addrs.build(params[:address]) # and so on... Check out the docs on ActiveRecord associations, specifically has_many. http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethod s.html unknown wrote:> Hi All, > I need your suggestions for the following. I am still learning RoR and> pardon my ignorance. > 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) %> > > Addresses_controller > ------------- > def list > @addresses = Address.find(:all, :conditions => [ "student_id= > (?)", params[:id]], :limit => 5) > @session["student_id"] = params[:id] End > > def create > @addresses = Address.new(params[:addresses]) > @addresses.student_id=@session["student_id"] > .............. > end > +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > > Are there any other way to do this. Any help appreciated. > Thanks in advance. > > Silvy Mathews-- Posted via http://www.ruby-forum.com/. _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
Silvy@funmail.com
2006-Jan-18 17:47 UTC
[Rails] Re: Inserting the parent Id to the child table
Looks like that''s true. Could you give me some more hint. The methods you have written should go to which method in the controller. I am trying to show the list of addresses associated with the student when clicked from the student''s list. My question is, if I need to add a new address, how do I get that id (not when I am trying to edit a particular address of the associated student). If you can give me some more hints, that would be wonderful. I looked at the api''s. Thanks Silvy Mathews -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Joshua Susser Sent: Wednesday, January 18, 2006 12:13 AM To: rails@lists.rubyonrails.org Subject: [Rails] Re: Inserting the parent Id to the child table It looks like you haven''t yet gotten the Rails way of doing this stuff. Don''t think so much about the db, but think more about the models. class Student < ActiveRecord::Base has_many :addresses ... end class Address < ActiveRecord::Base belongs_to :student ... end @student = Student.find(params[:id]) @addrs = @student.addresses @address = @addrs.build(params[:address]) # and so on... Check out the docs on ActiveRecord associations, specifically has_many. http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethod s.html unknown wrote:> Hi All, > I need your suggestions for the following. I am still learning RoR and> pardon my ignorance. > 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) %> > > Addresses_controller > ------------- > def list > @addresses = Address.find(:all, :conditions => [ "student_id= > (?)", params[:id]], :limit => 5) > @session["student_id"] = params[:id] End > > def create > @addresses = Address.new(params[:addresses]) > @addresses.student_id=@session["student_id"] > .............. > end > +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > > Are there any other way to do this. Any help appreciated. > Thanks in advance. > > Silvy Mathews-- Posted via http://www.ruby-forum.com/. _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails
Silvy@funmail.com
2006-Jan-19 18:42 UTC
[Rails] Inserting the parent Id to the child table
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. http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethod s.html 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) %> Addresses_controller ------------- def list @addresses = Address.find(:all, :conditions => [ "student_id= (?)", params[:id]], :limit => 5) @session["student_id"] = params[:id] End def create @addresses = Address.new(params[:addresses]) @addresses.student_id=@session["student_id"] .............. end +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 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 ... end class Address < ActiveRecord::Base belongs_to :student ... end @student = Student.find(params[:id]) @addrs = @student.addresses @address = @addrs.build(params[: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. Thanks Silvy Mathews
I''ll probably get flamed for saying this but @addresses = Address.new(params[:addresses]) @addresses.student_id=@session["student_id"] Is what I usually do if I''m associating something to another thing that already exists. If I have a student already in the DB, I''m just going to embed the id on the form or in session and directly add the value just like you did... In my opinion, (and that''s all it is), why should I create an instance of a student object (1 extra sql statement to populate the object) when I already have the ID of the record in my session or in my form? I''ll save some processing time and just associate it by hand. If I''m creating the student and address at the same time, then other methods are more appropriate. -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Silvy@funmail.com Sent: Thursday, January 19, 2006 12:41 PM To: rails@lists.rubyonrails.org Subject: [Rails] Inserting the parent Id to the child table 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. http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethod s.html 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) %> Addresses_controller ------------- def list @addresses = Address.find(:all, :conditions => [ "student_id= (?)", params[:id]], :limit => 5) @session["student_id"] = params[:id] End def create @addresses = Address.new(params[:addresses]) @addresses.student_id=@session["student_id"] .............. end +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 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 ... end class Address < ActiveRecord::Base belongs_to :student ... end @student = Student.find(params[:id]) @addrs = @student.addresses @address = @addrs.build(params[: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. Thanks Silvy Mathews _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails