Ruby/rails port of Cocoon/hibernate

12 views
Skip to first unread message

Andrew

unread,
Feb 26, 2007, 2:54:27 PM2/26/07
to Ruby on Rails: Talk
Hi,
I'm currently running an apache/jboss cocoon/flow/hibernate/ajax
paypal (directpayment) project and am looking into the possibility of
porting it across to ruby/rails. For that reason I would like to know
the following:

1. Can I call my java classes or would I be looking at a complete
rewrite in ruby?
2. How effective is ruby in terms of seperation of concerns regarding
design from content?
3. I have a number of hibernate maps accessing various postgresql
tables. Can I reverse engineer those tables into rails (maps?) ?
4. In cocoons mvc (flowscript) it is possible to instantiate and call
class methods from within the document at the same time as directing
flow. Is this possible in ruby/rails?

--
Regards

Andrew

Luke Ivers

unread,
Feb 26, 2007, 4:25:17 PM2/26/07
to rubyonra...@googlegroups.com
Ok, to start with, I don't really understand what #4 means, but I think I can address the other well enough:
#1: If the application is fairly complex, you'd probably be looking at a full re-write... however, there is a project called JRuby that is attempting to change that statement.  It allows you to run Ruby scripts on the JVM, and it allows you to reach out to existing Java code and call things in it... however, it's support for Rails isn't 100% atm... it can do basic stuff, moving on up into intermediate, but extremely complex code is better left outside of JRuby atm.
#2: Excellent.  It is on par with the best of any other framework I have ever known in this regard.
#3: Using Rails, you generally don't have to do much of anything to support already existing tables... Rails just reads in the schema info from the DB, and auto-generates all kinds of nifty things for you... the minor exception to this is that if you aren't using the default names that Rails likes in your DB (it wants the primary key auto-increment id field to be named id, etc) you have to specify them in your file like so:
class User < ActiveRecord::Base
  set_primary_key "userid"
end

Many people find On LAMP's Rolling with Ruby on Rails tutorial a great place to find out what Rails can offer: the revisited tutorial is particularly good in this respect:
http://www.onlamp.com/pub/a/onlamp/2006/12/14/revisiting-ruby-on-rails-revisited.html

Andrew Madu

unread,
Feb 26, 2007, 5:27:58 PM2/26/07
to rubyonra...@googlegroups.com
Hi Luke,
many thanks for the information you have provided thus far.

In regards to my question 4, an example might suffice: (myArtistDetails.js)

importClass(Packages.test.Artist); Flows equivalent of package test; import Artist;

function artistDetails() {

    try {
        artistID = cocoon.parameters.artistID;//either use defined or specified from dB
        artist = Artist.getArtist(artistID); //Artist id passed into class method
       
        artist_name = artist.getArtistName();//artist name is obtained from the Artist class
        artist_info = artist.getArtistInfo();// as above
       
    }catch(e) {
    }
   
    cocoon.sendPageAndWait("artistDetails2.xml",{"artistID":artistID,"artist_name":artist_name,"artist_info":artist_info}); //MVC: flow is directed to rtistDetails2.xml with associated values
}

In the above flow (MVC) document a java class is imported, its method interrogated and corresponding values passed onto the next document in line. How would this be handled in Ruby/Rails, such that an action on a web page causes artistDetaills() to be called and then directed on to artistDetails2.xml?

Am I correct in thinking that rails is a mapping technology much like hibernate?

--
Regards

Andrew

Luke Ivers

unread,
Feb 27, 2007, 7:51:51 AM2/27/07
to rubyonra...@googlegroups.com
It can be used as such, although I'm not sure that I would call that it's default application.
And you could do exactly what you're asking using AJAX and Rails's links to it... it is one of the major aspects of Rails that promotes it above and beyond most of the other frameworks available... you could even have it load up the artistDetails2.xml file into the page it's already on, instead of directing them to yet another page.

Scott

unread,
Feb 27, 2007, 9:09:31 AM2/27/07
to Ruby on Rails: Talk
Hi Andrew,

I come from a JBoss/J2EE/Java background and I have been working on my
first RoR project for the last several months... all-in-all it is a
pleasure! If you are new to RoR, it will take a bit to get used to the
syntax but once you do, it is amazing how quickly one can get things
done and do it in a structured, test-oriented way.

RoR has ActiveRecord that is analagous to Hibernate or EJB3. It
supports many of the features and maps relationships nicely. Some of
the features I don't see (someone please correct me if I am wrong)
are:
- lazy-loading/eager-loading ability
- object caching (like JBoss Cache)

In your situation, I wonder, rather than do a total immediate rewrite,
seeing you are using JBoss, why not keep JBoss as your object/db
broker with Hibernate and extend it out using Web Services and call
that from a RoR front-end? That way, you could migrate slowly rather
than wholesale.

Scott

On Feb 27, 7:51 am, "Luke Ivers" <technod...@gmail.com> wrote:
> It can be used as such, although I'm not sure that I would call that it's
> default application.
> And you could do exactly what you're asking using AJAX and Rails's links to
> it... it is one of the major aspects of Rails that promotes it above and
> beyond most of the other frameworks available... you could even have it load
> up the artistDetails2.xml file into the page it's already on, instead of
> directing them to yet another page.
>

> On 2/26/07, Andrew Madu <andrewm...@gmail.com> wrote:
>
>
>
> > Hi Luke,
> > many thanks for the information you have provided thus far.
>
> > In regards to my question 4, an example might suffice: (myArtistDetails.js
> > )
>
> > importClass(Packages.test.Artist); Flows equivalent of package test;
> > import Artist;
>
> > function artistDetails() {
>
> > try {
> > artistID = cocoon.parameters.artistID;//either use defined or
> > specified from dB
> > artist = Artist.getArtist(artistID); //Artist id passed into class
> > method
>
> > artist_name = artist.getArtistName();//artist name is obtained
> > from the Artist class
> > artist_info = artist.getArtistInfo();// as above
>
> > }catch(e) {
> > }
>

> > cocoon.sendPageAndWait("artistDetails2.xml",{"artistID":artistID,"artist_na me":artist_name,"artist_info":artist_info});


> > //MVC: flow is directed to rtistDetails2.xml with associated values
> > }
>
> > In the above flow (MVC) document a java class is imported, its method
> > interrogated and corresponding values passed onto the next document in line.
> > How would this be handled in Ruby/Rails, such that an action on a web page
> > causes artistDetaills() to be called and then directed on to
> > artistDetails2.xml?
>
> > Am I correct in thinking that rails is a mapping technology much like
> > hibernate?
>
> > --
> > Regards
>
> > Andrew
>

> > On 26/02/07, Luke Ivers <technod...@gmail.com> wrote:
>
> > > Ok, to start with, I don't really understand what #4 means, but I think
> > > I can address the other well enough:
> > > #1: If the application is fairly complex, you'd probably be looking at a
> > > full re-write... however, there is a project called JRuby that is attempting
> > > to change that statement. It allows you to run Ruby scripts on the JVM, and
> > > it allows you to reach out to existing Java code and call things in it...
> > > however, it's support for Rails isn't 100% atm... it can do basic stuff,
> > > moving on up into intermediate, but extremely complex code is better left
> > > outside of JRuby atm.
> > > #2: Excellent. It is on par with the best of any other framework I have
> > > ever known in this regard.
> > > #3: Using Rails, you generally don't have to do much of anything to
> > > support already existing tables... Rails just reads in the schema info from
> > > the DB, and auto-generates all kinds of nifty things for you... the minor
> > > exception to this is that if you aren't using the default names that Rails
> > > likes in your DB (it wants the primary key auto-increment id field to be
> > > named id, etc) you have to specify them in your file like so:
> > > class User < ActiveRecord::Base
> > > set_primary_key "userid"
> > > end
>
> > > Many people find On LAMP's Rolling with Ruby on Rails tutorial a great
> > > place to find out what Rails can offer: the revisited tutorial is
> > > particularly good in this respect:

> > >http://www.onlamp.com/pub/a/onlamp/2006/12/14/revisiting-ruby-on-rail...

Andrew Madu

unread,
Feb 27, 2007, 11:52:30 AM2/27/07
to rubyonra...@googlegroups.com
Hi Scott,
my appetite is definitely being whetted!! Please see my replies below:


I come from a JBoss/J2EE/Java background and I have been working on my
first RoR project for the last several months... all-in-all it is a
pleasure! If you are new to RoR, it will take a bit to get used to the
syntax but once you do, it is amazing how quickly one can get things
done and do it in a structured, test-oriented way.


From the various docs and examples I have seen it definitely does seem like a quick development environment!

RoR has ActiveRecord that is analagous to Hibernate or EJB3. It
supports many of the features and maps relationships nicely. Some of
the features I don't see (someone please correct me if I am wrong)
are:
- lazy-loading/eager-loading ability
- object caching (like JBoss Cache)


OK! I am using hibernate with ehcache handling lazy object caching. Can anyone else confirm whether ActiveRecord supports lazy-loading/object caching?

In your situation, I wonder, rather than do a total immediate rewrite,
seeing you are using JBoss, why not keep JBoss as your object/db
broker with Hibernate and extend it out using Web Services and call
that from a RoR front-end? That way, you could migrate slowly rather
than wholesale.


Great idea! I will look into this as a viable option.

The five main areas of concern to me are as follows:

1. Database.
I am using postgreSQL 8.1. Would rails be able to reverse engineer the required ActiveRecord maps from the dB schema? From what I understand of rails convention table names need to be pluralized. How would the reverse engineering process handle the fact that none of my table names are pluralized?!

2. Form validation
I am using Ajax to handle all form validation in cocoon. In cocoon form dev/validation would be handled by:

a. Specifying that you wish Ajax to handle validation as so:

<ft:form-template action=" login.kont" name="Form1" method="POST" ajax="true">

b.  Defining your form widgets in a seperate document as so:
                                  <ft:widget id="password">
                                      <fi:styling size="30" type="password" class="keyinbox" style="background-color:#C9C4BD;" />
                                  </ft:widget>

c. Defining your validation, against your widgest, in another document as so:

         <fd:field id="password" required="true">
          <fd:datatype base="string"/>
          <fd:validation>
            <fd:length min="5" max="20"/>
          </fd:validation>
         </fd:field>

3. Paypal API.

I have recently come across a ruby/rails Paypal (directpayment api) so this area is covered.

4. Image handling.

All artist images are stored in postgresql and read into the webapp as svg via Base64. Cocoon then allows for an svgTOpng or svgTOjpeg conversion before the image is rendered to the page. As an example I have various img calls defined as such:

<image src="artistImage/${artistID}/228/202/${stock_code}.svg" alt="${artist_name} - ${track_title}"/>

So in effect the img tag is making a call to the database which in turn return the appropriate binary information, holds it in svg format before converting it to either PNG or JPEG. How would ruby/rails handle this?

5. Email/Zip
I have a process where by the artist track files (mp3) a user may have ordered are pulled out from their order/order_items object, read in the file system directory and then zipped up. This zip is then emailed to the user. How would ruby/rails handle this process?

--
Regards

Andrew



Jason Roelofs

unread,
Feb 27, 2007, 12:14:47 PM2/27/07
to rubyonra...@googlegroups.com
My personal experience with Cocoon / JBoss / etc tells me to NEVER just port code from these frameworks to Rails. Rewrite the application in Rails, from scratch, redoing even the database if you are allowed to. The end result will be a pure-Rails site, which is vastly easier to develop and maintain than any Java solutions have ever been or will ever be. If you go trying to copy the way said Java solutions do things, you will end up with an unmaintainable mess, if you finish it at all. Rails works best when you let Rails do what it wants to do.

As for specific answers:

2) Look into RJS. This acts much in the same way as what Cocoon seems to do, defining partials of HTML code that get pushed to the client.

4) Check out the fleximage plugin: http://agilewebdevelopment.com/plugins/fleximage Should take care of all your needs with this.

Jason

Luke Ivers

unread,
Feb 27, 2007, 12:15:58 PM2/27/07
to rubyonra...@googlegroups.com
On 2/27/07, Scott <tamo...@gmail.com> wrote:

Hi Andrew,

I come from a JBoss/J2EE/Java background and I have been working on my
first RoR project for the last several months... all-in-all it is a
pleasure! If you are new to RoR, it will take a bit to get used to the
syntax but once you do, it is amazing how quickly one can get things
done and do it in a structured, test-oriented way.

RoR has ActiveRecord that is analagous to Hibernate or EJB3. It
supports many of the features and maps relationships nicely. Some of
the features I don't see (someone please correct me if I am wrong)
are:
- lazy-loading/eager-loading ability

Eager loading, sure.  You can put :include => blah on either your associations or in the actual find calls to eager load whatever you need.
Lazy loading:
By default, associations are lazy loaded.  You can change this behaviour as stated above.  There is no way, that I know of, to lazy load individual fields (other than doing something like using a view to get a copy of the table that doesn't have the fields, then doing direct sql queries to get the field in question).


- object caching (like JBoss Cache)

For this one, you can easily use memcache... Rails is set up to work excellently with it, and there's a lot of great help around to get you set up... it's not exactly a  Rails piece, but has been tightly integrated.

Also, in the current working version of Rails (called edge rails), all actions (read: functions that you call that generally end up rendering the html to the user) are cached by default... two identical queries will result in only one trip to the DB.

Luke Ivers

unread,
Feb 27, 2007, 12:20:33 PM2/27/07
to rubyonra...@googlegroups.com
OK! I am using hibernate with ehcache handling lazy object caching. Can anyone else confirm whether ActiveRecord supports lazy-loading/object caching?

See my other message


1. Database.
I am using postgreSQL 8.1 . Would rails be able to reverse engineer the required ActiveRecord maps from the dB schema? From what I understand of rails convention table names need to be pluralized. How would the reverse engineering process handle the fact that none of my table names are pluralized?!

Easy: you can tell your models specifically what table name to use, and it will just go ahead and use them.
In essence, you create a model... say you have a person table, and you want a Person model.
class Person < ActiveRecord::Base
  set_table_name 'person'
end
This will still draw in all your fields automatically and create accessors for them and etc... one other want to consider is the id field... Rails wants it to be named just flat out 'id', but a lot of people do something like 'person_id'... you can use set_id_field

2. Form validation
I am using Ajax to handle all form validation in cocoon. In cocoon form dev/validation would be handled by:

a. Specifying that you wish Ajax to handle validation as so:

<ft:form-template action=" login.kont" name="Form1" method="POST" ajax="true">

b.  Defining your form widgets in a seperate document as so:
                                  <ft:widget id="password">
                                      <fi:styling size="30" type="password" class="keyinbox" style="background-color:#C9C4BD;" />
                                  </ft:widget>

c. Defining your validation, against your widgest, in another document as so:

         <fd:field id="password" required="true">
          <fd:datatype base="string"/>
          <fd:validation>
            <fd:length min="5" max="20"/>
          </fd:validation>
         </fd:field>

Ajax + Rails = super, super, super easy.

3. Paypal API.

I have recently come across a ruby/rails Paypal (directpayment api) so this area is covered.

Definitely look into Active Merchant: it does Paypal and a TON of other stuff... so if you switch, you're still covered.

4. Image handling.

All artist images are stored in postgresql and read into the webapp as svg via Base64. Cocoon then allows for an svgTOpng or svgTOjpeg conversion before the image is rendered to the page. As an example I have various img calls defined as such:

<image src="artistImage/${artistID}/228/202/${stock_code}.svg" alt="${artist_name} - ${track_title}"/>

So in effect the img tag is making a call to the database which in turn return the appropriate binary information, holds it in svg format before converting it to either PNG or JPEG. How would ruby/rails handle this?

You completely got me on this one... I'm sure it's probably possible, but I don't have a clue.

5. Email/Zip
I have a process where by the artist track files (mp3) a user may have ordered are pulled out from their order/order_items object, read in the file system directory and then zipped up. This zip is then emailed to the user. How would ruby/rails handle this process?

ActionMailer is your friend...  ruby comes with standard zipping stuff built in, and ActionMailer is EXTREMELY easy to use to mail.

--
Regards

Andrew




Andrew Madu

unread,
Feb 27, 2007, 1:02:58 PM2/27/07
to rubyonra...@googlegroups.com
Hi jason,

My personal experience with Cocoon / JBoss / etc tells me to NEVER just port code from these frameworks to Rails.

Yes, I agree whole heartedly! I'm just trying to ascertain what what technology within the ruby/rails framework will allow me to achieve the same functionality as I currently have with the cocoon/hibernate framework

2) Look into RJS. This acts much in the same way as what Cocoon seems to do, defining partials of HTML code that get pushed to the client.


I will look into RJS.

4) Check out the fleximage plugin: http://agilewebdevelopment.com/plugins/fleximage Should take care of all your needs with this.


The plugin looks fantastic. Thanks for that headsup!

--
Regards

Andrew

Andrew Madu

unread,
Feb 27, 2007, 3:30:33 PM2/27/07
to rubyonra...@googlegroups.com
Hi Luke,
you are a diamond my friend!

All I need to know now is what is the best resource/documentation available in terms of hooking together a ruby/rails/ajax/rjs/activerecord project? Is there a ruby/rails/ajax/rjs/activerecord for dummies type publication available?

One further question if I may, in terms of persisting an objet retrieved from a dB table? Currently I retrieve the hibernate dB object and store it in a session object. I take it that ruby/rails/activerecord would function in much the same way?

I just came across some activerecord code:

class Order < ActiveRecord::Base
has_many :items
end

That's it?! One line of code to setup a one-to-many relationship between an order and order items table?!!! ;-)
--
Regards

Andrew

Max Muermann

unread,
Feb 27, 2007, 4:04:42 PM2/27/07
to rubyonra...@googlegroups.com
On 2/28/07, Andrew Madu <andre...@gmail.com> wrote:
> Hi Luke,
> you are a diamond my friend!
>
> All I need to know now is what is the best resource/documentation available
> in terms of hooking together a
> ruby/rails/ajax/rjs/activerecord project? Is there a
> ruby/rails/ajax/rjs/activerecord for dummies type
> publication available?
>

You'll want the Agile Web Development with Rails book from the
Pragmatic Programmers. IT covers all the technologies plus a lot more
and also contains a very useful reference to the rails framework.

--max

> One further question if I may, in terms of persisting an objet retrieved
> from a dB table? Currently I retrieve the hibernate dB object and store it
> in a session object. I take it that ruby/rails/activerecord would function
> in much the same way?
>

Yes, you can do that. However it's of course better pracice not to
store any application state (even temporary) in the session.

> I just came across some activerecord code:
>
> class Order < ActiveRecord::Base
> has_many :items
> end
>
> That's it?! One line of code to setup a one-to-many relationship between an
> order and order items table?!!! ;-)

Yes! And if you like that one, wait until you find out about the
really cool stuff ;)

--max

Ball, Donald A Jr (Library)

unread,
Feb 27, 2007, 4:22:13 PM2/27/07
to rubyonra...@googlegroups.com
 >  That's it?! One line of code to setup a one-to-many relationship between an order and order items table?!!! ;-)  
 
You'd probably also want another line of code to do the other half of the mapping:
 
class Item < ActiveRecord::Base
  belongs_to :order
end
 
So, two lines of code. Terrible, ennit.
 
- donald

Andrew Madu

unread,
Feb 27, 2007, 7:48:46 PM2/27/07
to rubyonra...@googlegroups.com
Hi Max,


You'll want the Agile Web Development with Rails book from the
Pragmatic Programmers. IT covers all the technologies plus a lot more
and also contains a very useful reference to the rails framework.

thanks for that recommendation. I will look it up.

--
Regards

Andrew

Andrew Madu

unread,
Feb 27, 2007, 8:13:08 PM2/27/07
to rubyonra...@googlegroups.com
Hi Donald,

 
class Item < ActiveRecord::Base
  belongs_to :order
end


Stop it! When I think how long it took me before I got a handle on hibernate map/classes!!!

2 lines?! It's just not right!!! ;-)

--
Regards

Andrew


Luke Ivers

unread,
Feb 27, 2007, 9:18:44 PM2/27/07
to rubyonra...@googlegroups.com
I love that everyone moving from a different framework (with the possible exception of things like Django) has these moments: the "oh my god it's so easy" ones.

You want to see something freaking _amazing_?
This is code I wrote on this list the other day, and it just flipped my lid that it was so easy to do this.
Say you want a tree of categories. such that you have a parent with multiple children, who can have multiple children, etc.

First, if you want the super easy route, do this
class Category < ActiveRecord::Base
  acts_as_tree
end

and put the field "parent_id" in your table... and you're done.
You can then do
category.children
category.children.parent
childcategory.siblings
etc, etc.

If you want to roll your own, it's one more line (of course, this doesn't give you all the fancy extra functions)

class Category < ActiveRecord::Base
  belongs_to :parent, class_name => 'Category', foreign_key => 'parent_id'
  has_many :sub_categories, class_name => 'Category', foreign_key => 'parent_id'
end

Done... you can now do:
category.sub_categories
childcategory.parent
childcategory.parent.sub_categories - self (returns siblings)

Amazing, eh?

Max Muermann

unread,
Feb 27, 2007, 9:50:27 PM2/27/07
to rubyonra...@googlegroups.com
On 2/28/07, Luke Ivers <techn...@gmail.com> wrote:
> I love that everyone moving from a different framework (with the possible
> exception of things like Django) has these moments: the "oh my god it's so
> easy" ones.
>

I also like the two-line ajax autocompletion:

in the view:

<%= autto_complete_field :user, :name %>

in the controller:

auto_complete_for :user, :name

Done.

Some of the plugins do amazing things too, such as acts_as_versioned.
One line of code to get a complete versioned history of every update
to a model, complete with rollback to a previous version. The code?

class WikiPage << ActiveRecord::Base
acts_as_versioned
end

and then

wiki_page.revert_to(wiki_page.version-1) # get previous version

;)

--max

peter royal

unread,
Feb 28, 2007, 2:06:08 AM2/28/07
to rubyonra...@googlegroups.com
On Feb 27, 2007, at 10:02 AM, Andrew Madu wrote:
> Yes, I agree whole heartedly! I'm just trying to ascertain what
> what technology within the ruby/rails framework will allow me to
> achieve the same functionality as I currently have with the cocoon/
> hibernate framework

there is no equivalent to the Cocoon flowscript (with the
continuations and all).. not that its a blocker, just be aware that
coding forms is different :)

-pete


--
(peter.royal|osi)@pobox.com - http://fotap.org/~osi

Scott

unread,
Feb 28, 2007, 8:04:23 AM2/28/07
to Ruby on Rails: Talk

On Feb 27, 9:18 pm, "Luke Ivers" <technod...@gmail.com> wrote:
> I love that everyone moving from a different framework (with the possible
> exception of things like Django) has these moments: the "oh my god it's so
> easy" ones.

Yeah. I was excited when EJB3 came out after years of EJB2/xDoclet.
Now, I won't even consider working on anything except RoR. Unless, I
get paid enough, of course. ;-)

That is very cool. I have a similar thing in the app I am working on
now, I am going to give that a whirl. Thanks!

Andrew Madu

unread,
Feb 28, 2007, 12:32:05 PM2/28/07
to rubyonra...@googlegroups.com
Hi,
does the postgres-pr interface support SSL and if so is it set to support SSL by default or do I need to configure a ruby/rails document to activate it?

--
Regards

Andrew

Andrew Madu

unread,
Feb 28, 2007, 6:31:15 PM2/28/07
to rubyonra...@googlegroups.com
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"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    " http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="test">
    <class name="Artist" table="artisttbl" lazy="false">
        <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>
</hibernate-mapping>

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?

--
Regards

Andrew

Andrew Madu

unread,
Mar 1, 2007, 6:19:02 AM3/1/07
to rubyonra...@googlegroups.com
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?

Jason Roelofs

unread,
Mar 1, 2007, 8:58:48 AM3/1/07
to rubyonra...@googlegroups.com
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

Andrew Madu

unread,
Mar 1, 2007, 9:26:11 AM3/1/07
to rubyonra...@googlegroups.com
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?

--
Regards

Andrew

Mark Thomas

unread,
Mar 1, 2007, 9:57:05 AM3/1/07
to Ruby on Rails: Talk
On Mar 1, 9:26 am, "Andrew Madu" <andrewm...@gmail.com> wrote:
> 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

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?

Reply all
Reply to author
Forward
0 new messages