I have two user types in my application: Investors and Advisors, they both have a separate model and table in database (Investor model, Advisor model, Investors table and Advisors table).
I need to create a table to keep advisor's profile data and a different table to keep investor's profile(they have completely different fields in profile, so i cant merge into a single table). What name should i give to it? If i call it investor_profile - then every time i want to get a data from it i have to call it investor->investor_profile->field_name but thats seems bad to type investor twice.
Any advises for a proper name? Or maybe a trick to make it investor->profile->field_name?
Thank you.
You might store different tables for investor_profiles and advisor_profiles with separate models InvestorProfile, AdvisorProfile (which both might inherit from a base Profile class, assuming they have at least a tiny bit of overlap).
But in your model associations, use the :class_name option to hide the _profiles:
class Investor < ActiveRecord::Base
has_one :profile, :class_name => "InvestorProfile"
end
class Advisor < ActiveRecord::Base
has_one :profile, :class_name => "AdvisorProfile"
end
# And since the profiles probably overlap in some way
# a profile base class which the other 2 profile classes extend
class Profile < ActiveRecord::Base
# base options like name, address, etc...
end
class InvestorProfile < Profile
# Investor-specific stuff
end
class AdvisorProfile < Profile
# Advisor-specific stuff
end
In practice then, you can refer to it as self.profile:
# Use it as
adv = Advisor.new
puts adv.profile.inspect
See the ActiveRecord::Associations documentation for descriptions of the options.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With