Two has_one's?


#1

Hi,

I’d like to associate two model objects such as

class Pair < ActiveRecord::Base
has_one :good_member, :class_name=>‘Member’
has_one :bad_member, :class_name=>‘Member’
end

class Member < ActiveRecord::Base
belongs_to :pair
end

so that a pair has two members where one is good and the other is bad.

As far as I understand, the association information would be kept in
the “member” database table as a foreign key to the associated “pair”
record, e.g. member_id

Since the association information is not kept within the “pair” table

  • where it would be possible to distinguish between the good and the
    bad member, how can I do the above association?

It would be quite comfortable to declare this kind of relationship in
the style above.

Many thanks in advance,
Siegi


#2

so that a pair has two members where one is good and the other is bad.

As far as I understand, the association information would be kept in
the “member” database table as a foreign key to the associated “pair”
record, e.g. member_id

Since the association information is not kept within the “pair” table

  • where it would be possible to distinguish between the good and the
    bad member, how can I do the above association?

sql table has columns:

memberable_type, :string

memberagble_id, :integer

class Member < ActiveRecord::Base
belongs_to :memberable, :polymorphic => true
end

class GoodMember < Member
end

class BadMember < Member
end

class Pair < ActiveRecord::Base
has_one :good_member, :as => :memberable
has_one :bad_member, :as => :memberable
end