ruby on rails 4 - ActiveRecord - Display columns from multiple associations -
i have user, user_profile , profile_type models. user has_many user_profiles, user_profile belongs_to user , belongs_to profile_type , profile_type has_many user_profiles.
i have been reading on how work having problems figuring out , appreciated.
i know sql statement (freehand sql, excuse mistakes), want use activerecord.
select up.id, u.user_id, pt.connection_type user u join user_profile on u.user_id = up.user_id join profile_type pt on pt.profile_type_id = up.profile_type_id u.username = "test"
i want return nested user_profile objects associated user want user_profile object contain profile_type.connection_type instead of profile_type.profile_id.
currently, if this,
user.user_profiles.all
add iterate through nested user_profiles returned, output:
{ :id :user_id :profile_type_id }
what want is:
{ :id :user_id :profile_type.connection_type (instead of profile_type.profile_type_id) }
user model
class user < activerecord::base has_many :user_profiles, autosave: true has_many :account_settings, autosave: true end
user_profile model
class userprofile < activerecord::base belongs_to :user belongs_to :profile_type end
user profile type model
class profiletype < activerecord::base has_many :user_profiles end
try this:
user.account_settings.select("profile_type.*, profile_type.connection_type").all
Comments
Post a Comment