# Describing a "replaces" relationship within the same model

**URL:** <https://www.ruby-forum.com/t/describing-a-replaces-relationship-within-the-same-model/64235>\
**Category:** Rails\
**Created:** [June 23, 2006, 12:31pm UTC](https://www.ruby-forum.com/t/describing-a-replaces-relationship-within-the-same-model/64235 "2006-06-23T12:31:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nikolai\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/df705f/32.png) [@Nikolai\_W](https://www.ruby-forum.com/u/Nikolai_W)\
**Post date:** [June 23, 2006, 12:31pm UTC](https://www.ruby-forum.com/t/describing-a-replaces-relationship-within-the-same-model/64235/1 "2006-06-23T12:31:50Z")

</div>

Hi!

I would like to describe the relationships between two objects of a  
model, where one of the objects replaces the other. I want to be able  
to tell if one object has been replaced and, if so, by what object; and  
vice versa what object an object replaces, if any. My idea was the  
following:

create\_table :events do |t|  
t.column :replacement\_id, :integer  
t.column :description, :text, :null =\> false  
end

create\_table :replacements, {:id =\> false} do |t|  
t.column :original\_id, :integer, :null =\> false  
t.column :new\_id, :integer, :null =\> false  
t.column :date, :date, :null =\> false  
end

Is this a reasonable model and, if so, how should I link Event and  
Replacement together?

My idea for the above was: An Event can belong to a Replacement and a  
Replacement has an original Event and a new Event. One can thus tell if  
an Event replaces or is replaced by another Event by looking at what  
member of a Replacement an Event belongs to, i.e., if it is equal to  
Replacement#original or Replacement#new. I also want to know the date  
when the replacement was made.

Any suggestions are welcome.

nikolai

---

<div class="post-metadata">

**Author:** ![Nikolai\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/df705f/32.png) [@Nikolai\_W](https://www.ruby-forum.com/u/Nikolai_W)\
**Post date:** [June 23, 2006, 1:46pm UTC](https://www.ruby-forum.com/t/describing-a-replaces-relationship-within-the-same-model/64235/2 "2006-06-23T13:46:56Z")

</div>

Nikolai W. wrote:

> create\_table :replacements, {:id =\> false} do |t|

Hm, I guess doing :id =\> false here isn’t right, so it should be

create\_table :replacements do |t|

After realizing this the set up becomes the following:

create\_table :events do |t|  
t.column :replacement\_id, :integer  
t.column :description, :text, :null =\> false  
end

create\_table :replacements do |t|  
t.column :original\_id, :integer, :null =\> false  
t.column :new\_id, :integer, :null =\> false  
t.column :date, :date, :null =\> false  
end

And the models will be:

class Event \< ActiveRecord::Base  
belongs\_to :replacement  
end

class Replacement \< ActiveRecord::Base  
belongs\_to :original,  
:class\_name =\> “Event”  
:foreign\_key =\> “original\_id”  
belongs\_to :new,  
:class\_name =\> “Event”  
:foreign\_key =\> “new\_id”  
end

Does that seem reasonable?

I guess what confused me is that I always seem to read “belongs\_to” as  
“belongs to”, which makes sense for Event, instead of “references”,  
which makes sense for Replacement, as is sometimes what you’re trying to  
achieve.

nikolai
