ActiveRecord with a weird DB Schema

Hi everyone,

I’m having a bit of difficulty wrapping my head around this schema.
Basically there are no ‘normal’ foreign key. It’s all text…

PACKAGES

PKGPATH (primary key)
NAME
DESCRIPTION

CATEGORIES

NAME
PKGPATH (sort of foreign key that references table PACKAGES)

P.S. Yes everything is in CAPS

And now I’m trying to fit this in active record…

Now a simple :foreign_key => ‘PKGPATH’ doesn’t work because the
generated SQL tries to match it to an ‘id’ column:

class Package < ActiveRecord::Base
belongs_to :category, :foreign_key => ‘PKGPATH’
end

When I do ruby script/console:
p = Package.find(:first) <=== OK
p.category <==== NOT OK

p.category generates the following SQL: SELECT * FROM categories WHERE
(categories.id = ‘archivers/arc’) LIMIT 1

I was thinking either of using :conditions on the belongs_to side and
finder_sql on the has_many side…but up until now
belongs_to->:conditions tells me that the constant PKGPATH is
uninitialized… :frowning:

Here’s what I tried with the :condition

class Port < ActiveRecord::Base
belongs_to :category, :foreign_key => ‘PKGPATH’, :conditions =>
‘categories.PKGPATH = #{PKGPATH}’
end

Any help would be greatly appreciated…

JD

On 11/1/06, Jean-Daniel B. [email protected] wrote:

Hi everyone,

I’m having a bit of difficulty wrapping my head around this schema.
Basically there are no ‘normal’ foreign key. It’s all text…

Keys can be varchar.

What might make your brain hurt less is using views on those tables
with the rails naming conventions. If, of course, your SQL variant
supports views.


_Deirdre http://deirdre.net/