ruby on rails - ActiveRecord::Relation returning different class object -
i have rails 4.2 webapp. has typical user
model based off devise
people sign up, log in, , thing.
at same time, have model called partner
, connected remote database called partner_data
containing table called 'user
'.
in rails console, getting object of user
class when should partner. ideas on what's happening?
> abc_partner = partner.where(id:200) => #<activerecord::relation [#<user id: 262, email: "abc@abc.xyz", created_at: ...>]> > abc_partner.class => partner::activerecord_relation > abc_partner.first.class => user(id: integer, email: string, encrypted_password: string ... )
a temporary fix i've done is
abc_partner.becomes(partner) => #<partner id:262 name: 'john doe', partners_since:'2013-03-19', location:'new york, ny', company:'abcd corp.'>
however: have asset
model that's connected same remote database, using table of same name 'asset
' , works:
> abc_asset = asset.where(id:101) => #<activerecord::relation [#<asset id: 213, name: "arc", description: "my arc", created_at: "2015-03-13", owner_login: "john doe", ...>]> > abc_asset.class => asset::activerecord_relation > abc_asset.first.class => asset(id: integer, name: string, description: text, created_at: datetime, ...)
i've experimented changing name 'user
' table of partners_data
database various things 'partner_user
' or 'omg_user
' see if table name made difference, , nothing changed.
i notice abc_partner.class , abc_asset both return different..., partner::activerecord_relation , asset::activerecord_relation...
classes:
class partner < activerecord::base establish_connection(:my_partners_db) self.table_name = 'user' belongs_to: :partner_list end class asset < activerecord::base establish_connection(:my_partners_db) self.table_name = 'asset' end
over in database.yml
:
my_partners_db: adapter: mysql encoding: utf8 host: 1.1.1.1 database: partner_data username: some_username password: some_password pool: 7 port: 3317
could have fact have
self.table_name = 'user'
in partner
model? docs here
if you're trying make partner
type of user
, consider activerecord inheritance.
Comments
Post a Comment