Forum: Ruby on Rails Please Help with single table inheritance relationships

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2958660cbb27954a8b6acd6c797cb1f8?d=identicon&s=25 ChrisD (Guest)
on 2006-06-06 16:03
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
Dc730e56d34292b3a5c73925cf5a9d39?d=identicon&s=25 ajaya agrawalla (ajay)
on 2006-06-07 04:14
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
This topic is locked and can not be replied to.