Good timing, just got it fixed (non-smelly)and you're pretty close to
the mark.

The answer?  has_many :through - something I'd never taking much of a
look at, but it is actually a pretty nice feature (even in other
circumstances).

Basically this just allows me to create a model class which represents
the relationship.  And since I have a model class for it I can
explicitly specify the database to connect to.

For posterity sake, here's the code:

class Pushbroom::UsersRolesRelationship < ActiveRecord::Base

  establish_connection :pushbroom
  set_table_name :users_roles

  belongs_to :user_account
  belongs_to :user_role
end

class Pushbroom::UserAccount < ActiveRecord::Base

  establish_connection :pushbroom
  set_table_name :user_account
  set_primary_key :id

  has_many :users_roles_relationships
  has_many :user_roles, :through
=> :users_roles_relationships, :source => :user_role
end

class Pushbroom::UserRole < ActiveRecord::Base

  establish_connection :pushbroom
  set_table_name :user_role
  set_primary_key :id

  has_many :users_roles_relationships
  has_many :user_accounts, :through
=> :users_roles_relationships, :source => :user_account
end

And is used thusly:

def add_subscription_plan_roles_to_pb_user_account(pb_user_account)
    roles_granted =
pb_user_account.user.subscriptions.first.subscription_plan.roles_granted
    pb_user_account.user_roles = roles_granted
end

Thanks a ton folks for helping me get this train moving again!  All my
tests are passing and it seems to be working, but if you see something
wrong, please do still let me know.

Thanks!
Gerald


On Apr 14, 6:47 am, Matt Jones <[email protected]> wrote:
> On Apr 13, 1:20 pm, Gerald Anderson <[email protected]> wrote:
>
>
>
>
>
>
>
>
>
> > Grr, have had two meetings in the middle of all this, forgive my lack
> > of coherence.  Bottom line is it looks like it instantiates the engine
> > as the rails database and with no way (that I know of) of specifying
> > which db to look at for the relation table I'm kind of screwed.  It
> > seems to ignore the fact that I'm using :join_table =>
> > 'pb_prod.users_roles" for the relationship definition (pb_prod is the
> > external database) and isn't picking up which database to use from
> > either object (connection :pushbroom).
>
> > So it looks to me, at the moment, as though I'm stuck.  It does bother
> > me though that it LOADS the relationships fine.  Just seems to be a
> > problem on inserts.
>
> > Again, just spouting what I've found and desperately looking for a
> > solution.  Would prefer to find out I'm just being an idiot instead of
> > having to implement some solution with seriously high code smell.
>
> Short of finding and squashing the bug, one not-too-smelly alternative
> might be to make a real model for users_roles, like this:
>
> class UsersRoles < ActiveRecord::Base
>   establish_connection :pushbroom
>   set_primary_key nil
>
>   belongs_to :user_account, :class_name => 'PushBroom::UserAccount'
>   belongs_to :user_role, :class_name => 'PushBroom::UserRole'
> end
>
> then you could replace the habtm call with a set of
> has_many :throughs, which shouldn't have the same problem (since
> you've explicitly told Rails the join table is on the :pushbroom
> connection).
>
> --Matt Jones

-- 
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 [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to