Hi Everyone, I''m updating an existing web-app (perl/mysql) and have decided to give Ruby/Rails a try for a new module. I am new to both, and I have already fallen in love with Ruby (being a perl/c/assembly person). First let me say that the app is really odd, not at all like most web-apps. It is focused on viewing and searching HUGE databases, with very complicated relationships determined primarily by search optimization requirements and domain issues. I have some questions about models. Earlier posts on the list talk about a model as being an encapsulation of business logic and the data it operates on; But ActiveRecord seems geared towards representing a single object/table. 1. Can I make ActiveRecord objects that do not have a single underlying table, but rather are a collection of my custom SQL statements for gluing together the tables? Is there a better way to do this? 2. Most business logic involves multiple tables, for example storing an order. When each AR object represents a single table, where to you put the multi-table logic? I''ve read that putting in the controller is bad. 3. Some things like users and accounts fit well with the rails paradigm. So say I have two tables: users // login, passwords, permissions user_key contact_key user_contact // addresses, phone numbers, names contact_key user_key Those tables have two different relationships. There is the obvious 1 user == 1 contact: users.contact_key=user_contact.contact_key But I also store all the previous contact info: user_contact.user_key = users.user_key gives me all previous contact sets. Those relationships seem to conflict when using the has_one, belongs_to, etc. methods. Any suggestions on how to handle this? 4. Can I make 2 different active record objects that access the same table (for defining the relationships above)? 5. Should I just give up on ActiveRecord and do my own thing with SQL (like I have being doing for years in perl)? 6. Can "models" be generic, non-ActiveRecord objects? 7. What exactly does the "model" method do in the controller? it''s in the examples but seems to be unnessecary. 8. I put a "tableBuilder" method in application_helper.rb. It seems that I can call if from any view, which is cool, but how is it that I can just call "tableBuilder(...)" from inside a .rhtml view without specifying which object it is in? I totally don''t get how the name spaces work... Thanks so much, and please have patience, I have been all over the docs, source and mailing list but still don''t understand how to approach a complicated project with rails. Thanks in advance, Lee -- Naxos Technology
Hi Lee, On 3.2.2005, at 07:03, LN wrote:> 1. Can I make ActiveRecord objects that do not have a > single underlying table, but rather are a collection of > my custom SQL statements for gluing together the tables? > Is there a better way to do this?If you can make an updateable view out of your queries, you should be fine. There''s an article about this in the wiki: http://wiki.rubyonrails.com/rails/show/HowtoUsePostgresViewsAsTables. It talks about PostgreSQL, but you should be able to extrapolate the info to other databases, too.> > 2. Most business logic involves multiple tables, for > example storing an order. When each AR object represents > a single table, where to you put the multi-table logic? > I''ve read that putting in the controller is bad. > > 3. Some things like users and accounts fit well with > the rails paradigm. So say I have two tables: > > users // login, passwords, permissions > user_key > contact_key > > user_contact // addresses, phone numbers, names > contact_key > user_key > > Those tables have two different relationships. There > is the obvious 1 user == 1 contact: > > users.contact_key=user_contact.contact_key > > But I also store all the previous contact info: > > user_contact.user_key = users.user_key > > gives me all previous contact sets. > > Those relationships seem to conflict when using the > has_one, belongs_to, etc. methods. Any suggestions on > how to handle this?So this doesn''t work(?): class User def primary_key() "user_key" end belongs_to :current_contact, :class_name => "UserContact", :foreign_key => "contact_key" # refers to user.contact_key has_many :old_contacts, :class_name => "UserContact", :foreign_key => "user_key" # refers to user_contact.user_key end class UserContact def primary_key() "contact_key" end has_one :current_user, :class_name => "User", :foreign_key => "contact_key" # user.contact_key belongs_to :legacy_user, :class_name => "User", :foreign_key => "user_key" #usercontact.user_key end So you should be able to make multiple associations between same objects, as long as they have different names (like :current_contact, :old_contacts...).> 7. What exactly does the "model" method do in the controller? > it''s in the examples but seems to be unnessecary.ActiveRecord can nowadays find and include all the model files automatically so it''s not needed anymore (as long as you have Rails 0.9.x, I think)> > 8. I put a "tableBuilder" method in application_helper.rb. > It seems that I can call if from any view, which is cool, > but how is it that I can just call "tableBuilder(...)" from inside > a .rhtml view without specifying which object it is in? I > totally don''t get how the name spaces work...This is not authorative, but I think application_helper and [controllername]_helper.rb are automatically included in the controller so these methods will be mixed in to the controller class. See http://www.rubycentral.com/book/tut_modules.html#S2 for more info on mixins.> Thanks so much, and please have patience, I have been all > over the docs, source and mailing list but still don''t understand > how to approach a complicated project with rails.No problem, we will be more than happy to hear about your experiences. //jarkko -- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On Feb 2, 2005, at 11:03 PM, Jarkko Laine wrote:> Hi Lee, > > On 3.2.2005, at 07:03, LN wrote: >> 1. Can I make ActiveRecord objects that do not have a >> single underlying table, but rather are a collection of >> my custom SQL statements for gluing together the tables? >> Is there a better way to do this? > > If you can make an updateable view out of your queries, you should be > fine. There''s an article about this in the wiki: > http://wiki.rubyonrails.com/rails/show/HowtoUsePostgresViewsAsTables. > It talks about PostgreSQL, but you should be able to extrapolate the > info to other databases, too.You may want to consider FrontBase if you''re going to be using lots of views. FrontBase has excellent, fast view support -- much better than PostgreSQL IMO. I''m currently finishing up the AR adaptor for Frontbase, so if you''re interested, just drop me a line. Best, Eric> >> >> 2. Most business logic involves multiple tables, for >> example storing an order. When each AR object represents >> a single table, where to you put the multi-table logic? >> I''ve read that putting in the controller is bad. >> >> 3. Some things like users and accounts fit well with >> the rails paradigm. So say I have two tables: >> >> users // login, passwords, permissions >> user_key >> contact_key >> >> user_contact // addresses, phone numbers, names >> contact_key >> user_key >> >> Those tables have two different relationships. There >> is the obvious 1 user == 1 contact: >> >> users.contact_key=user_contact.contact_key >> >> But I also store all the previous contact info: >> >> user_contact.user_key = users.user_key >> >> gives me all previous contact sets. >> >> Those relationships seem to conflict when using the >> has_one, belongs_to, etc. methods. Any suggestions on >> how to handle this? > > So this doesn''t work(?): > > class User > def primary_key() > "user_key" > end > > belongs_to :current_contact, :class_name => "UserContact", > :foreign_key => "contact_key" # refers to user.contact_key > has_many :old_contacts, :class_name => "UserContact", :foreign_key > => "user_key" # refers to user_contact.user_key > end > > class UserContact > def primary_key() > "contact_key" > end > > has_one :current_user, :class_name => "User", :foreign_key => > "contact_key" # user.contact_key > belongs_to :legacy_user, :class_name => "User", :foreign_key => > "user_key" #usercontact.user_key > end > > So you should be able to make multiple associations between same > objects, as long as they have different names (like :current_contact, > :old_contacts...). > >> 7. What exactly does the "model" method do in the controller? >> it''s in the examples but seems to be unnessecary. > > ActiveRecord can nowadays find and include all the model files > automatically so it''s not needed anymore (as long as you have Rails > 0.9.x, I think) > >> >> 8. I put a "tableBuilder" method in application_helper.rb. >> It seems that I can call if from any view, which is cool, >> but how is it that I can just call "tableBuilder(...)" from >> inside >> a .rhtml view without specifying which object it is in? I >> totally don''t get how the name spaces work... > > This is not authorative, but I think application_helper and > [controllername]_helper.rb are automatically included in the > controller so these methods will be mixed in to the controller class. > See http://www.rubycentral.com/book/tut_modules.html#S2 for more info > on mixins. > > >> Thanks so much, and please have patience, I have been all >> over the docs, source and mailing list but still don''t understand >> how to approach a complicated project with rails. > > No problem, we will be more than happy to hear about your experiences. > > //jarkko > > -- > Jarkko Laine > http://jlaine.net > http://odesign.fi > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Hi Eric, Thanks for the tip. I''m stuck with MySQL because this is a legacy app. But I''ll take a look at frontbase for the next proj. -Lee On Thu, 3 Feb 2005 08:40:39 -0800, Eric Ocean wrote> On Feb 2, 2005, at 11:03 PM, Jarkko Laine wrote: > > > Hi Lee, > > > > On 3.2.2005, at 07:03, LN wrote: > >> 1. Can I make ActiveRecord objects that do not have a > >> single underlying table, but rather are a collection of > >> my custom SQL statements for gluing together the tables? > >> Is there a better way to do this? > > > > If you can make an updateable view out of your queries, you should be > > fine. There''s an article about this in the wiki: > > http://wiki.rubyonrails.com/rails/show/HowtoUsePostgresViewsAsTables. > > It talks about PostgreSQL, but you should be able to extrapolate the > > info to other databases, too. > > You may want to consider FrontBase if you''re going to be using lots > of views. FrontBase has excellent, fast view support -- much better > than PostgreSQL IMO. I''m currently finishing up the AR adaptor for > Frontbase, so if you''re interested, just drop me a line. > > Best, Eric > > > > >> > >> 2. Most business logic involves multiple tables, for > >> example storing an order. When each AR object represents > >> a single table, where to you put the multi-table logic? > >> I''ve read that putting in the controller is bad. > >> > >> 3. Some things like users and accounts fit well with > >> the rails paradigm. So say I have two tables: > >> > >> users // login, passwords, permissions > >> user_key > >> contact_key > >> > >> user_contact // addresses, phone numbers, names > >> contact_key > >> user_key > >> > >> Those tables have two different relationships. There > >> is the obvious 1 user == 1 contact: > >> > >> users.contact_key=user_contact.contact_key > >> > >> But I also store all the previous contact info: > >> > >> user_contact.user_key = users.user_key > >> > >> gives me all previous contact sets. > >> > >> Those relationships seem to conflict when using the > >> has_one, belongs_to, etc. methods. Any suggestions on > >> how to handle this? > > > > So this doesn''t work(?): > > > > class User > > def primary_key() > > "user_key" > > end > > > > belongs_to :current_contact, :class_name => "UserContact", > > :foreign_key => "contact_key" # refers to user.contact_key > > has_many :old_contacts, :class_name => "UserContact", :foreign_key > > => "user_key" # refers to user_contact.user_key > > end > > > > class UserContact > > def primary_key() > > "contact_key" > > end > > > > has_one :current_user, :class_name => "User", :foreign_key => > > "contact_key" # user.contact_key > > belongs_to :legacy_user, :class_name => "User", :foreign_key => > > "user_key" #usercontact.user_key > > end > > > > So you should be able to make multiple associations between same > > objects, as long as they have different names (like :current_contact, > > :old_contacts...). > > > >> 7. What exactly does the "model" method do in the controller? > >> it''s in the examples but seems to be unnessecary. > > > > ActiveRecord can nowadays find and include all the model files > > automatically so it''s not needed anymore (as long as you have Rails > > 0.9.x, I think) > > > >> > >> 8. I put a "tableBuilder" method in application_helper.rb. > >> It seems that I can call if from any view, which is cool, > >> but how is it that I can just call "tableBuilder(...)" from > >> inside > >> a .rhtml view without specifying which object it is in? I > >> totally don''t get how the name spaces work... > > > > This is not authorative, but I think application_helper and > > [controllername]_helper.rb are automatically included in the > > controller so these methods will be mixed in to the controller class. > > See http://www.rubycentral.com/book/tut_modules.html#S2 for more info > > on mixins. > > > > > >> Thanks so much, and please have patience, I have been all > >> over the docs, source and mailing list but still don''t understand > >> how to approach a complicated project with rails. > > > > No problem, we will be more than happy to hear about your experiences. > > > > //jarkko > > > > -- > > Jarkko Laine > > http://jlaine.net > > http://odesign.fi > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-- Naxos Technology
Jarkko: Thanks for the answers. I did not realizer that you could name associations independently of classes. Your example worked perfectly. For everyone else: Unfortunately I can''t use views (old version of mysql; also some queries are dynamically built). Does anyone know if I can make a abstract model that calls other table-backed models to do it''s dirty work? Also I noticed that when using associations, rails will generate 2 separate database queries, rather than using a join. I see why it happens (rails doesn''t know what I will want from another table until I ask for it), but it will get slow for bigger stuff. Is there anyway to force rails to use joins? thanks Lee