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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs