Hi Alexey, On Fri, May 6, 2011 at 5:21 AM, Alexey Muranov <[email protected]> wrote: > Hello, > > i wonder if the following is possible in Rails (seems like not), and if > not, whether such database structure is discouraged for some good > reason. > > I *roughly* want to have > > class Payment < ActiveRecord::Base > has_one :purchase > end > > where purchase can be either TicketPurchase, or MembershipPurchase, so > to have also > > class TicketPurchase < ActiveRecord::Base > belongs_to :payment > end > > class MembershipPurchase < ActiveRecord::Base > belongs_to :payment > end
Depending on your constraints, i.e., whether or not you have control of the schema, you might flip the association. class TicketPurchase < ActiveRecord::Base has_one :payment # this could just as easily be a has_many if you need to support split tenders end class MembershipPurchase < ActiveRecord::Base has_one :payment end class Payment < ActiveRecord::Base belongs_to :ticket_purchase belongs_to :membership_purchase end Alternatively, or perhaps in addition, the purchase hierarchy your explanation touches on looks like a pretty classic use-case for STI (http://martinfowler.com/eaaCatalog/singleTableInheritance.html) HTH, Bill -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

