user419017
user419017

Reputation:

How to connect multiple tables to a single model?

I have quite a complex model which has many attached columns - around 40, and so I have split them up into multiple tables:

business
business_details

I was hoping I won't have to create a BusinessDetails model and use relationship Business has_one BusinessDetails.

How do I connect business to business_details and access both through the model Business?

Upvotes: 1

Views: 332

Answers (1)

Marlin Pierce
Marlin Pierce

Reputation: 10089

Are you looking for something like this?

class BusinessDetails < ActiveRecord::Base
  belongs_to :business
end

class Business < ActiveRecord::Base
  has_one :business_details

  delegate :bd_field1, :bd_field2, :to => :business_details, :allow_nil => true
end

Upvotes: 3

Related Questions