Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a proper naming convention for related tables and models in rails 3

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.

like image 540
Tamik Soziev Avatar asked Dec 29 '25 01:12

Tamik Soziev


1 Answers

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.

like image 118
Michael Berkowski Avatar answered Dec 31 '25 18:12

Michael Berkowski