mysql - How can I select columns return with joins method in Active Record without associations? -


i working on rails model, mapped on external (non default) database table.

i need make join obtain data need instantiate object.

i using active record's functions:

self.joins("left outer join accounts on accounts.code = customers.code").select("customers.code, accounts.email, accounts.firstname, accounts.password_hash").where('accounts.code = ?', my_param)[0] 

the model class is:

class user::customer < user    self.establish_connection :external_db   self.table_name = "customers"     attr_accessor :password_hash   attr_accessor :firstname   attr_accessor :email end 

this query does:

select customers.code, accounts.email, accounts.firstname, accounts.password_hash `customers` left outer join accounts on accounts.code = customers.code (accounts.code = '11111111') 

so query right, object:

#<user::customer id: nil, code: "11111111"> 

has attributes of table. expected that, because set attr_accessor of other attributes, rails map result of query object, surely missing , haven't undestand how work.

edit: i've forgotten mention fact have no association, can't use solution.

question

having attr_accessor you'll able attributes by:

@user.email 

you need specify to_s instance method achieve want:

class user::customer < user   #...   def to_s     "#{password_hash}, #{firstname}, #{email}"   end end 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -