Loading...
If the page contents do not appear, it may mean that JavaScript is disabled in your browser. Please enable JavaScript to view this.
An Article from Aaron's Article ArchiveRuby ActiveRecord Newbie Question (Answered) and a PHP Cake/Rails Question Photo: Sego Lily on KolobIPv4You are not logged in. Click here to log in. | |
Use Google to search aarongifford.com:
Here is one of my web log entries, perhaps from my Yakkity Yak page, What's New page, or one of my Astounding Adventures from my Geocaching section: Ruby ActiveRecord Newbie Question (Answered) and a PHP Cake/Rails Question
Wednesday, 15 August 2007 2:14 PM MDT
Yakkity Yak
I'm using Ruby's ActiveRecord single-table inheritance to define several types of objects. I've got a burning question that perhaps someone may be able to answer for me.
[NOTE: I've since answered this question, though I would still love to hear an answer to the question at the bottom of this post.] Read on, and if you can help me, please send me a message. First, an example of an ActiveRecord set of classes:
In the above, class Bar and the two subclasses Baz and Bop are all be stored in a single database table, with a type column that will determine which class each row in the table belongs to.
What if I want to make sure that for any particular set of Bar , Baz , and Bop instances that exist as rows in the database belonging to a particular Foo instance, that only a single Bop instance can exist at any one time in the database.
I can think of many ways that are quite cumbersome to enforce this restriction, but nothing simple and elegant. It would be nice to even be able to create a new Bop instance that was not yet saved to the database, and perhaps on save, to enforce the single instance rule, delete any previous Bop instances from the database.
Any suggestions? UPDATE: (Later, the same day as the above question was first posted) With a little experimentation with the has_one relationship, I answered my own question. I'd previously believed I couldn't use it with the above class structure, but in fact I can. So my modified Foo class looks like:
Of course this is only enforced if I don't bypass ActiveRecord's normal operation and set the foo_id field of a Bop class instance directly, but instead do something like foo.bop = Bop.new to replace the Foo 's single Bop instance.
There's nothing like yet-even-more online searching of documentation to answer one's own questions. END-of-UPDATE Oh, and here's another only vaguely related question: Does anyone know if PHP Cake's implementation of ActiveRecord is 1:1 compatible with Ruby's ActiveRecord such that a PHP application and a Rails application could share a single back-end database data store? Think of it, the PHP app. creates an object, saves it, then sometime later, the Ruby app. accessess and modifies the object. It would be a thing of beauty. UPDATE II: Electric Boogaloo Now that I'm more familiar with Cake, it turns out it isn't really a PHP implementation of the Active Record though overall it shares some similar behaviors. So it definitely is NOT 1:1 compatible with Ruby's Active Record, though I'm sure with a little extra code one could get an application in one to work with objects from an application that uses the other. | |
Copyright © 1993-2012 - Aaron D. Gifford - All Rights Reserved |