views:

27

answers:

1

I have a data model in which I would like to have an item that has a description that can be edited. I would like to also keep track of all edits to the item. I am running into issues with my current strategy, which is:

class Item < ActiveRecord::Base
  has_one  :current_edit,
           :class_name => "Edit",
           :foreign_key => "current_edit_id"
  has_many :edits
end

class Edit < ActiveRecord::Base
  belongs_to :item
end

Can the Item have multiple associations to the same class like this?

I was thinking that I should switch to keeping track of the edit version in the Edit object and then just sorting the has_many relationship base on this version.

+1  A: 

Yes it can. But you have to use belongs_to not has_one. Then your models have to look like:

(Item, current_edit_id, ...)
(Edit, item_id, ....)

The rails doc explains this in more detail: Is it a belongs_to or has_one association?

gregor
That was all it took, thanks.
lillq