views:

19

answers:

1

I have these models:

class Bill < ActiveRecord::Base
  has_many :calls
  has_many :text_messages
end

class Call < ActiveRecord::Base
  belongs_to :bill
end

class TextMessage < ActiveRecord::Base
  belongs_to :bill
end

Now, in my domain calls and text messages are both "the same kind of thing" -- i.e., they're both "bill items". So I'd like some_bill.bill_items to return all calls and text messages associated with that bill. What's the best way to do this?

A: 

You could create a new model, BillItems and insert it into your relationship chain. Then you could access all bill items collectively or individual call/text messages via a has many through relationship. Another possibility would be to look into single table inheritance.

Jimmy Cuadra
what do you mean by "insert it into your relationship chain"? So Bills would have_many BillItems, every BillItem has_one call / text_message (via polymorphic associations?), and Bills have_many :calls / :text_messages :through => :bill_item?
Horace Loeb
@Horace You got it.
Jimmy Cuadra