How to join on a field other than parent table's id?

Hi,

I am putting xml into a database. I have one table, products, that
has_many product_features.

I would like to use the product table’s id in the product_features
table, and join on that, but the problem is that the product table’s
id does not exist until the product is in the database.

I would prefer to join on a different field – a string I make up
randomly, and keep track of. How do I do that within my product and
product_feature models? I can’t find anything in the documentation
that says anything about how to join on anything other than the parent
table’s id.

I do see the :source method, but this appears to allow you to make an
alias for a table name, but still forces you to use the parent table’s
id.

Help appreciated.

Charlie

when I first started using rails active_records I struggled because I
had previously used a java O/R mapping tool that used a guid generator
to build primary keys as soon as you instantiated a model object. This
really simplifies dealing with foreign key relationships in complex
models. But I think you can get used to living with the way rails
deals with this. I use foreign key constraints in the database, which
it seems many rails developers do not (?). I did some testing early
on to figure out how rails works and these are the notes I kept on
this:

-add child to parent and save the parent => saves both
-add child to parent and save the child => error, null value in fk
-set childs parent and save child => saves both
-set childs parent and save parent => only saves parent

these seem like fairly reasonable rules to live by and I’ve gotten
used to it. Are you having problems along these lines? Do you use fk
constraints?

I would really recommend not trying to add a different key value if
possible. I suspect there are other ways to solve your problem.

(warning: i’m no rails guru!)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs