You can specify what the foreign key is when making the association. For
documentation see here:
http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html
For example, in the class of the model having the belongs_to
association, do this:
belongs_to :problem_codes, :foreign_key => ''code''
Michael King wrote:> I am creating a webapp to track problem analysis, for that I have a
> table of problem codes which looks like:
> id integer autoinc
> hex varchar(10)
> name varchar(50)
> code bigint
>
> I have some other tables which have foreign keys to this table. My
> boss wants me to add in some auto categorizing of generated problems.
> The generated problems come from an existing table, which does have a
> field like problem_codes.code above.
>
> Is there some way I can define a belongs_to relationship with the
> current table setup or do I have to change my problem_codes table to
> use code as the primary key?
>
> If I have to change the primary key is there an easy way of changing
> the old foreign keys to reference the new foreign key?
>
> - Michael
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails