views:

33

answers:

3

Hi I have a relation between 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.

+2  A: 

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
fl00r
A: 

Sorry i misundersttod the Question.So i edited it.i Agree with fl00r's Answer.

Salil
A: 

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"
xaver23
ok, but it's not has_many relationship, but has_one :)
fl00r