views:

25

answers:

2

I want a has_many relationship described below

class User < ActiveRecord::Base
  has_many :mcollections, :foreign_key=>'obj_id'
end

Below is definition of table mcollections

create table mcollections (
  id                     int            not null auto_increment,
  obj_id                 varchar(255)   not null,
  category               varchar(255)   not null,
);

The :foreign_key is NOT A SINGLE FIELD on table mcollections. Foreign key has to be a combination of 2 fields (obj_id + category). How can I specify this in User class?

A: 

Cant you do it another way?

Its not the best practice to use multi-column foreign keys in rails...

Lichtamberg
A: 

I don't see the sense of using foreign keys here. A foreign key should be the primary key of another table. Neither obj_id nor category could be used as foreign key because they are not part of the primary key.

brainfck