Hi, Something that I have noticed is that every class that inherits from ActiveRecord::Base has a database table of its own. However what if I don''t want a class to have its own table, but to actually serve as a proxy to initialize other entries? For instance as an example, let''s have a form with the fields for "student name", "student id number", "parent''s name", "street 1", "street 2", "town", "postcode", "state". There will be (suppose) 2 underlying database tables that will be populated as a result of this form submission, a table "student_record" that has "id", "name", and "id_number" (not to be confused with "id", it is more like "passport number"), as well as another table "contact" with the fields "parent_name", "street_1", "street_2", "town", "postcode", "state" as well as "student_record_id" which is a foreign key pointing to the id of the associated "student_record". Now, strictly speaking it can be argued that all of these entries can be put into a single table for simplicity''s sake, but supposing there are students with multiple parental contacts (perhaps separated, or they have holiday addresses, etc) and the system is meant to accomodate them, hence the provision for multiple contacts. How is it possible to have a "front end" class that acts and works just like an object by itself (ie, maybe a "student" class), but actually have no underlying database table of its own and instead exists by pulling records from associated databases? -- Posted via http://www.ruby-forum.com/.
I''m not sure if this matches what you need, but this is the first thing that popped in my head. So this means it''s probably more complicated than it needs to be :) In the student model use: class Student < ActiveRecord::Base #this should handle the student_id reference as long as #the contact table has a student_id field... #but you can change this default association #http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000471 has_many :contact ...rest of model code end In the controller you can use: def edit @student = Student.find(@params[:student_id]) end In the view you can use something like: <%=text_field_tag("student_name",@student.name)%> loop through the contacts: <% @student.contact.each{|contact| %> <%=text_field_tag("street_#{contact.id}",contact.street_1)%> <% } %> using the #{contact_id} to differentiate between the params. any help at all? -andy -- Andrew Stone -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060118/049c9a9d/attachment.html
Woei Shyang
2006-Jan-18 00:31 UTC
[Rails] Re: An object that initializes multiple objects?
Thanks, that''s what I suspected too. Initially I thought rails has a way of doing it so I can get something for free, guess not :( Just to clear up something, so in the contact class there''d be a statement belongs_to :student, and when I add a new contact to a student I use the statement student.contact << Contact.new(:street1 => "etc etc...) Will I need to specify a :Student => student when creating the new contact? Or will rails do that for free? -- Posted via http://www.ruby-forum.com/.
Andrew Stone
2006-Jan-18 00:41 UTC
[Rails] Re: An object that initializes multiple objects?
> > I use the statement student.contact << Contact.new(:street1 => "etc > etc...) > >I believe that using the above statement will handle student association in the new contact for you. I know this works for has_and_belongs_to_many, just haven''t had the chance to use has_many yet. -andy -- Andrew Stone -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060118/1894ed2a/attachment.html
Woei Shyang
2006-Jan-18 06:32 UTC
[Rails] Re: Re: An object that initializes multiple objects?
Andrew Stone wrote:> I believe that using the above statement will handle student association > in > the new contact for you. I know this works for has_and_belongs_to_many, > just haven''t had the chance to use has_many yet. > > -andyThanks Andrew, it does work :) However one issue that I have been trying to smooth out which is also discussed at http://www.ruby-forum.com/topic/51957, let''s extend on our example again and now this Contact class validates input postcodes by matching them with the correct town and state names. The trouble with using << is it seems like error messages that results from a child object failing to save does not seem to show up to the parent class. Is there a way to actually communicate the validation error messages between the different .save methods? -- Posted via http://www.ruby-forum.com/.
Andrew Stone
2006-Jan-18 13:54 UTC
[Rails] Re: Re: An object that initializes multiple objects?
> The trouble with using << is it seems like error messages that results > from a child object failing to save does not seem to show up to the > parent class. Is there a way to actually communicate the validation > error messages between the different .save methods?I haven''t ran into this scenario, so I don''t have a well thought out solution. Unless there''s a mechanism for this in Rails already, I would think you would have to test for error messages after each save of the child and then add a message to the parent. There is probably a slick way to do this, but I don''t know it. :( -andy -- Andrew Stone -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060118/ad29e89b/attachment-0001.html
Woei Shyang
2006-Jan-18 21:04 UTC
[Rails] Re: Re: Re: An object that initializes multiple objects?
Andrew Stone wrote:> I haven''t ran into this scenario, so I don''t have a well thought out > solution. Unless there''s a mechanism for this in Rails already, I would > think you would have to test for error messages after each save of the > child > and then add a message to the parent. There is probably a slick way to > do > this, but I don''t know it. :( > > -andyHmmm, but the flaw with that approach is that if you are calling .save on a child object created via the append operator to a newly created parent, it can only be saved when the parent is saved (the parent.id is not present until it actually gets into the database). Wonder if there is some way to override the default save behaviour? -- Posted via http://www.ruby-forum.com/.