开发者

Building a wiki like data model in rails question

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_开发者_Go百科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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜