Reputation: 59
I have three models User, Individual and Business. I also created three tables one for each. My individual and user table are identical so i inherited from user.rb My problem was when i came to the business.rb, i was able to access all of the parent attributes of User (example: first_name) but i could not access the model specific attributes (example: company_name) which are in the businesses table.
class User < ApplicationRecord
# This is the user model
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable, :lockable, :timeoutable
enum status: {unverified: 0, verified: 1}
end
# This is the business model
class Business < User
end
individual.rb
# # This is the individual model
class Individual < User
end
schema.rb
# This is the schema for all the models
ActiveRecord::Schema.define(version: 2018_06_09_091056) do
create_table "businesses", force: :cascade do |t|
t.string "company_address"
t.string "company_name"
t.string "company_phone_number"
t.text "documents"
end
create_table "individuals", force: :cascade do |t|
end
create_table "users", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.integer "status", default: 0
t.string "date_of_birth"
t.text "passport"
t.string "country"
t.string "personal_address"
t.text "recovery_photo"
t.string "type"
t.string "email", default: "", null: false
# etc..
end
end
Upvotes: 2
Views: 806
Reputation: 28285
It's called Single Table Inheritance for a reason: All of the data is stored in a single table.
By writing class Individual < User
, you are storing this model's data within the users
table.
The only distinguishing feature about these records is that individual.type == 'Individual'
.
The individuals
and businesses
tables, in your current design, are never used.
Now with that said, here's how I would change it:
class User < ApplicationRecord
# This is the user model
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable, :lockable, :timeoutable
enum status: {unverified: 0, verified: 1}
end
# app/models/individual.rb
class Individual < User
end
# app/models/business.rb
class Business < User
has_one :business_attributes
end
# app/models/business_attributes.rb
class BusinessAttributes < ApplicationRecord
belongs_to :business
end
# db/schema.rb
ActiveRecord::Schema.define(version: 2018_06_09_091056) do
create_table "business_attributes", force: :cascade do |t|
t.string "company_address"
t.string "company_name"
t.string "company_phone_number"
t.text "documents"
end
create_table "users", force: :cascade do |t|
t.string "first_name"
t.string "last_name"
t.integer "status", default: 0
t.string "date_of_birth"
t.text "passport"
t.string "country"
t.string "personal_address"
t.text "recovery_photo"
t.string "type"
t.string "email", default: "", null: false
# etc..
end
end
Upvotes: 5