TamiNicks
2012-Oct-07  10:17 UTC
Rails Expert Help Please: ActiveRecord & Multiple Database connections
Okay, so, first, complete newbie...which means that I would GREATLY 
appreciate any help on this one.  Have been pulled, abruptly, in the middle 
of trying to get something deployed, quickly...and, being a newbie, this is 
difficult for me, but, based on everything that I''ve read, it actually
is
fairly easy.
I''ve done this every which way but Sunday...this is my latest 
attempt...take a look and let me know what steps I''ve missed...  Thank
You!
I''m attempting to connect to first the prime database for this
particular
web application, confirm or deny if a user''s email address exists, and,
if
it does exist, redirect the member over to one of our partner sites, while 
also keeping a count on the ''users'' table to ensure that the
user doesn''t
try to access the partner site more then 2x.
This is working just fine, no problems...  That''s the good news...
If the application can''t find the user on the application''s
prime database
site.  It now must connect to a secondary database and look for the user on 
that database in the ''members'' table.  If the email address
does exist, it
increments the ''redirect_count'' and then redirects the member
over to one
of our partner sites.  If the member has reach the max allotted access to 
the partner site, a message is flashed advising the user of such.
So, now for the code...note that the code provided, and the help needed, is 
specific to connecting to, and validating (and updating) the members table.
This is the model file that I''ve created for the purpose of the
connecting
to the secondary database, and validating  / updating the corresponding 
members table.
********************************************************************************************
class DealCore < ActiveRecord::Base
  conn2 = { :adapter => ''mysql'',
    :encoding => ''utf8'',
    :reconnect => ''true'',
    :database => ''deals_qa'',
    :username => ''xxxxxx'',
    :password => ''xxxxxx'',
    :host => ''xx.xx.xx.xx'',
    :port => ''xxxx''
  }
  self.abstract_class = true
  establish_connection (conn2["deals_#{RAILS_ENV}"])
  class User < DealCore
    def clone_to_deals
      m = Users.find_by_email(@email)
      m.redirect_count += 1
      m.save
    end
    class << self
      def email_exists?(email)
        find_by_email(email)
      end
}
...and this is the code snippet that I created to call these
''secondary''
database classes:
if @user = DealCore::User.email_exists?(@email)
                member = @user.clone_to_deals
                @Redirect_Flag = [0,1,2].include? member.redirect_count
                @Costco_Flag = true
              end
Little help...pretty please!
Thanks!
Tami
-- 
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-talk/-/bGe_nMR9tVYJ.
For more options, visit https://groups.google.com/groups/opt_out.