Please Help with single table inheritance relationships


#1

I’ve been searching the web, wikis, and more, and I haven’t turned up
examples of how to have multiple entities in a single-table inheritance
related to anything.

I have an addresses table, but multiple entities can have Addresses:
both Person (which has 2 addresses) and Retailer. I’ve been told that I
need to use single-table inheritance, and this was my attempt:

migrate file

create_table ‘addresses’ do |t|
t.column ‘person_id’ :integer
t.column ‘type’ :string

end

Generated model from rails’ scaffolding

class Address < ActiveRecord::Base
end

Manually added these 3 classes

class ShippingAddress < Address
belongs_to :person
end
class BillingAddress < Address
belongs_to :person
end
class ShipFromAddress < Address
belongs_to :retailer
def retailer_id= (input)
person_id = input;
end
def retailer_id
person_id
end
end

Made these changes:

class Person < ActiveRecord::Base
has_one :shipping_address
has_one :billing_address
end
class Retailer < ActiveRecord::Base
has_one :ship_from_address
end


Now, this all makes some sense to me, but I don’t know that it makes
Rails sense (and there is no validator or Relationship Manager to tell
me if it is valid syntax or set of commands), and it does not run:

SQLite3::SQLException: SQL logic error or missing database: SELECT
count(*) AS count_all FROM addresses

Since the table does exist I’m stuck. What’s my next step? Anyone know
a CVS or SVN with an example which might clear things up for me?

Thanks,

-Chris


#2

ChrisD wrote:

I’ve been searching the web, wikis, and more, and I haven’t turned up
examples of how to have multiple entities in a single-table inheritance
related to anything.

I have an addresses table, but multiple entities can have Addresses:
both Person (which has 2 addresses) and Retailer. I’ve been told that I
need to use single-table inheritance, and this was my attempt:

migrate file

create_table ‘addresses’ do |t|
t.column ‘person_id’ :integer
t.column ‘type’ :string

end

Generated model from rails’ scaffolding

class Address < ActiveRecord::Base
end

Manually added these 3 classes

class ShippingAddress < Address
belongs_to :person
end
class BillingAddress < Address
belongs_to :person
end
class ShipFromAddress < Address
belongs_to :retailer
def retailer_id= (input)
person_id = input;
end
def retailer_id
person_id
end
end

Made these changes:

class Person < ActiveRecord::Base
has_one :shipping_address
has_one :billing_address
end
class Retailer < ActiveRecord::Base
has_one :ship_from_address
end


Now, this all makes some sense to me, but I don’t know that it makes
Rails sense (and there is no validator or Relationship Manager to tell
me if it is valid syntax or set of commands), and it does not run:

SQLite3::SQLException: SQL logic error or missing database: SELECT
count(*) AS count_all FROM addresses

Since the table does exist I’m stuck. What’s my next step? Anyone know
a CVS or SVN with an example which might clear things up for me?

Thanks,

-Chris

What you need is Polymorphic Assciation.

has_many :address, :as => addressable

Search for Polymorpic Assciation. It’s part of Rails 1.1

ajay