Ruby/rails port of Cocoon/hibernate

Hi, I have just come across an article which states that primary keys must be auto-increment and id(which I know can be overridden). So when I am dealing, in hibernate, with the following:

<?xml version=" 1.0" encoding="UTF-8"?>
    <cache usage="read-write"/>
    <id name="ID" column="artist_id">
        <generator class="assigned"/>
    </id>
    <version name="Version" column="version" type="integer"/>

    <property name="ArtistName" column="artist_name" type="string" not-null="true"/>
    <property name="ArtistInfo" column="artist_info" type="string" not-null="true"/>

    <property name="StockID" column="stock_id" type="string"/>
    <property name="FeaturedArtist" column="featured_artist" type="boolean" not-null="true"/>

    <set name="ArtistImages" inverse="true" lazy="true" cascade="all-delete-orphan">
        <cache usage="read-write"/>
        <key column="artist_id"/>

        <one-to-many class="ArtistImages"/>   
    </set>
</class>

which basically says that the primary id is ‘assigned’ and mapped to the artist_id column of the artisttbl with a one to many relationship against the artistimagestbl, the artist_id being the Foreign key, how would this be reflect in ActiveRecord? The database schema alraeady exists, I just want to see how ruby/rails/activerecord would reflect this in actual code.

What actual rails/activerecord command would be needed to say 'go away to this dB and create all the required activrecord classes based on the dB schema?

Hi, No worries with this one guys. I have a handle on this now.

I would still like to know how to ensure that a primary key can be ‘assigned’. The following code would associate an auto_increment key correct?:

class Usertbl < ActiveRecord::Base set_primary_key “user_id” end

So how do I make the primary key assignable?

Assignable meaning not auto_increment and you take care of the value yourself? ActiveRecord doesn’t really like that. It basically requires that it take care of handling the primary key. I’m not sure if there’s a plugin to work around this, but you’ll probably have to jump into AR itself if you start getting crazy with the keys.

Unless of course someone knows a proper way of doing this.

Jason

Hi Jason,

Assignable meaning not auto_increment and you take care of the value yourself?

Yes.

ActiveRecord doesn’t really like that. It basically requires that it take care of handling the primary key.

Hmmm…not so good! Let’s say you have an order object with corresponding order items FKey referenced by order_id. If the order table is auto generating its own id and each order item is also doing the same…nothing will work, plus I can’t change the table structure as it is needed by another system!!!

I’m not sure if there’s a plugin to work around this, but you’ll probably have to jump into AR itself if you start getting crazy with the keys.

AR? What’s that?

This is a very standard, basic model setup. It will work just fine in Rails. Generate models for order and order item, then add to order.rb:

  has_many :order_items

add to order_item.rb

  belongs_to :order

and Rails will work as you describe. The curious thing is why you don't trust Hibernate to perform id management for you? Are you doing something unusual?