Ball, Donald A Jr \(Library\)
2007-Jan-22 22:16 UTC
multiple database users for rails app?
Hey guys. Per my IT department''s policies, I need to have two separate database users for my rails database - one for public intarweb functionality, one for authenticated users'' functionality. The naive way to accomodate this would be to break the app up into two separate apps, but since they share the same models and resources, DRY suggests this isn''t the proper thing to do. Ideally, I''d like to have two separate database.yml files, and choose between them on a controller-by-controller basis. If this is a good strategy, how might I go about doing it? If not, how would y''all suggest I structure my app? - donald --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> Ideally, I''d like to have two separate database.yml files, and choose > between them on a controller-by-controller basis. If this is a good > strategy, how might I go about doing it? If not, how would y''all suggest > I structure my app? > > - donaldWhy not have a log-in action and if a user is successfully authenticated, store something in the session. Then you can check for the existence of this session variable and act accordingly. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hello Donald, 2007/1/22, Ball, Donald A Jr (Library) <donald.ball@nashville.gov>:> Hey guys. Per my IT department's policies, I need to have two separate > database users for my rails database - one for public intarweb > functionality, one for authenticated users' functionality. The naive way > to accomodate this would be to break the app up into two separate apps, > but since they share the same models and resources, DRY suggests this > isn't the proper thing to do.I have done something similar previously. I have a site which has a backend and frontend. The backend has admin functionnality, while the frontend has different views. I shared the models using svn:externals, but you could do the same using a plugin or piston. Hope that helps ! -- François Beausoleil http://blog.teksol.info/ http://piston.rubyforge.org/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ball, Donald A Jr \(Library\)
2007-Jan-22 22:24 UTC
Re: multiple database users for rails app?
> > Ideally, I''d like to have two separate database.yml files, > and choose > > between them on a controller-by-controller basis. If this is a good > > strategy, how might I go about doing it? If not, how would y''all > > suggest I structure my app? > > > > - donald > > Why not have a log-in action and if a user is successfully > authenticated, store something in the session. Then you can > check for the existence of this session variable and act accordingly.That''s easy enough to do, but how do I "act accordingly"? How do I tell my controller that, based on the presence of this session property, connect to the database using these credentials instead of those? - donald --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Sounds to me like you want two DB connections, one for each user. Use the session variable to choose which connection to use. Multiple connections to one db should be equivalent to connecting to multiple databases: http://www.google.com/search?q=rails+multiple+databases --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ball, Donald A Jr \(Library\)
2007-Jan-23 18:05 UTC
Re: multiple database users for rails app?
> Sounds to me like you want two DB connections, one for each > user. Use the session variable to choose which connection to > use. Multiple connections to one db should be equivalent to > connecting to multiple > databases: http://www.google.com/search?q=rails+multiple+databasesYes, that''s what I want to do, but this doesn''t seem to be a common practice, or at least a well-documented common practice. I''ll see what I can do to help fix the latter. :) It would seem the pragmatic thing to do would be to define a parallel set of database targets in database.yml (admin_development, admin_production, etc.) and in the admin controllers, using a before_filter, call ActiveRecord::Base.establish_connection with the admin users'' credentials. Three questions come up: 1. Can I access the rails Configuration object at request-time? I mean, I could parse database.yml manually, but that''s redundant. Alternately, I could store a reference to the Configuration object in environment.rb. Any suggestions? 2. If I manually call ActiveRecord::Base.establish_connection, should I close the original connection first? Should I close the newly created connection myself in an after_filter? What should I do in case of exceptions? 3. Do the answers vary depending on if I running in a FastCGI, CGI, or Mongrel environment? Any tips are greatly appreciated. Any pointers towards comprehensive documentation of the rails'' application and requests lifecycles would be helpful too. Cheers. - doald --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---