I’m in Rails 3.2.2 and I have a Variant class:
class Variant < ActiveRecord::Base
has_one :twin_variant
end
I’d like to be able to associate two objects from this class to became “twins”.
I’d like to create:
v1 = Variant.new
v1.name = "Fantastic variant"
v1.save
Then I’d like to have a method to create a twin variant:
v2 = Variant.new
v2.name = "Fantastic variant twin"
v2.save
v1.twin_variant = v2
Then the 2 objects should become associated with each other, so that:
v1.twin_variant
=> v2
v2.twin_variant
=> v1
Is this possible? How should I build the association?
Given the following model
You can setup the cyclic relationship with
and you can check this with