开发者

rails: has_many with 2 relations fields

Hi I have a relation be开发者_如何学运维tween 2 models. The models are "Category" and "Page". I have now the use case that I need 2 Pages/Category. How can I write this? The fields in Category are "page_id" and "page_en_id".

I'm not sure which is the best solution for this, I only know the belongs_to and has_many solution with foreign key option which makes here no sense for me.


I'm not sure, what you're looking for, but it looks like that:

class Page < ActiveRecord::Base
  has_many :categories
  has_many :en_categories, :foreign_key => 'page_en_id', :class_name => 'Category'
end

class Category < ActiveRecord::Base
  belongs_to :page
end


Ok this is my solution now, thanks guys:

Page

has_one :category
has_one :category_en, :foreign_key => 'page_en_id', :class_name => 'Page'

Category

belongs_to :page
belongs_to :page_en, :class_name => "Page"
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜