ChongQing Xiao
2004-Nov-30 17:07 UTC
RE: question about rails regarding handling more than onetable in one view
Hi, Jarkko: Thanks for the quick reply. I understand I can use ActiveRecord to specify the relation between the table as you have suggested, The part that I have trouble to understand is 1. Since I have a third table ProvWorkAtDept, do I need to specify this table somewhere when I define Provider or Dept? 2. If the provider view has both the provider name and several depts the provider works on, how that data will be handled by the Provider model or controller, Do I need write code in Provider model (or the controller) to call Provider#Dept<<Dept (or Provider#Dept.Build, Create) to save the relation to ProvWorkAtDept table Or rails will handle it automatically? It will be great if you give me some explanation between provider view, model and the controller regarding how the depts will be filed. Thanks again chong ________________________________ From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Jarkko Laine Sent: Tuesday, November 30, 2004 10:42 AM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails] question about rails regarding handling more than onetable in one view Hi Chong, Rails has indeed magic to take care of both one-to-many and many-to-many relationships. Please read http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethod s.html which has an extensive explanation of how associations work in Rails. For your example: class Provider > ActiveRecord::Base has_and_belongs_to_many :depts end class Dept > ActiveRecord::Base has_and_belongs_to_many :providers end class Order > ActiveRecord::Base has_many :orderitems end class Orderitem > ActiveRecord::Base belongs_to :order end HTH, Jarkko On 30.11.2004, at 18:16, ChongQing Xiao wrote: Hi, Everyone I am just starting to look at the rail and really like how it works. One question I can not figure out is how rails will solve the following problem. I have 3 tables Table 1 - Provider table ProvID Name Table 2 - Dept Table DeptId Name Table 3 - ProvWorkAtDept ProvID DeptID Table 3 is the relation table between provider and dept and one provider can work at more than one dept. Now if I want to have a view for provider which will let the user edit specific provider - name - dept lists (the user can enters multi dept for this particular provider) If I have a provider model, how the model will handle the dept lists since the dept lists is in a separate table, do I need to write Code to handle it or rails have some magic way to handle this problem. Also, How rails will handle master detail relation which is common in database application (such as order and order items)? Thanks a lot chong _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails -- Jarkko Laine http://jlaine.net _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Jarkko Laine
2004-Nov-30 17:19 UTC
Re: question about rails regarding handling more than onetable in one view
Hi Chong, On 30.11.2004, at 19:07, ChongQing Xiao wrote:> > 1. Since I have a third table ProvWorkAtDept, do I need to specify > this table somewhere when I define Provider or Dept?From http://api.rubyonrails.org/classes/ActiveRecord/Associations/ ClassMethods.html#M000121: has_and_belongs_to_many Associates two classes via an intermediate join table. Unless the join table is explicitly specified as an option, it is guessed using the lexical order of the class names. So a join between Developer and Project will give the default join table name of "developers_projects" because "D" outranks "P".> > 2. If the provider view has both the provider name and several depts > the provider works on, how that data will be handled by the Provider > model or controller,You will have a method called Provider#depts which will return an array (I think) consisting of Dept objects. Plus many other nifty methods that you can find from that same address above.> > Do I need write code in Provider model (or the controller) to call > Provider#Dept<<Dept (or Provider#Dept.Build, Create) to save the > relation to ProvWorkAtDept table Or rails will handle it > automatically?Provider#depts<< @deptobject will do what is needed. You don''t have to write any additional code.> It will be great if you give me some explanation between provider > view, model and the controller regarding how the depts will be filed.This all has been part of ActiveRecord (=model). You will of course call those functions (like Provider#depts<< @deptobject) in your controllers. Hope this made things a bit clearer. //jarkko -- Jarkko Laine http://jlaine.net _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails