Rails: Okay, that's just stupid
Jul. 16th, 2008 06:19 pmI have long asserted that if you can't handle SQL or Javascript, you shouldn't be programming rails. And I don't mean a little bit; you ought to be able to eat, sleep, and breathe Javascript, HTML, CSS, SQL, and Ruby before you begin to sell yourself as a Rails programmer.
I am not a Rails programmer. Even so, I ought to be able to do simple things. Yet today I was stymied and went for my sledgehammer, the SQL statement.
Here's the layout: Stories exist. Stories belong to serials. Stories may optionally belong to arcs. Arcs theoretically belong to serials, but there is the "no arc" setting which, unfortunately, is global across the Journal Entries and my Other Stories archive, since there are plenty of standalone Journal Entries and most of the Other Stories stories are also standalone rather than complex. (That's a joke. Zoner will get it.)
So I wanted to be able to find all arcs that belong to a series. That's a simple statement in SQL: select distinct arcs.id, arcs.title, arcs.blurb from arcs, stories where stories.serial_id = 1 and arcs.id = stories.arc_id order by stories.pubdate. You see, arcs and serials only have a relationship through stories, not with each other.
There seems to be no clear way to do this in rails. It should be something like Arcs.find(...), but that didn't work at all.
This is on top of my discovering that yes, you can define your own pluralization rules, but you cannot override the ones inside Rails. "Series" will always be unpluralizable. If you try that, it works great until you trip over a call that dives beneath your application to the appserver layer, at which point Rails's internal pluralization rules take over and you're screwed.
I am not a Rails programmer. Even so, I ought to be able to do simple things. Yet today I was stymied and went for my sledgehammer, the SQL statement.
Here's the layout: Stories exist. Stories belong to serials. Stories may optionally belong to arcs. Arcs theoretically belong to serials, but there is the "no arc" setting which, unfortunately, is global across the Journal Entries and my Other Stories archive, since there are plenty of standalone Journal Entries and most of the Other Stories stories are also standalone rather than complex. (That's a joke. Zoner will get it.)
So I wanted to be able to find all arcs that belong to a series. That's a simple statement in SQL: select distinct arcs.id, arcs.title, arcs.blurb from arcs, stories where stories.serial_id = 1 and arcs.id = stories.arc_id order by stories.pubdate. You see, arcs and serials only have a relationship through stories, not with each other.
There seems to be no clear way to do this in rails. It should be something like Arcs.find(...), but that didn't work at all.
This is on top of my discovering that yes, you can define your own pluralization rules, but you cannot override the ones inside Rails. "Series" will always be unpluralizable. If you try that, it works great until you trip over a call that dives beneath your application to the appserver layer, at which point Rails's internal pluralization rules take over and you're screwed.
no subject
Date: 2008-07-17 02:31 am (UTC)select arcs.id, arcs.title, arcs.blurb, max(stories.pubdate) as pubdate from arcs join stories on arcs.id = stories.arc_id group by arcs.id order by pubdate;(modulo syntax quirks and errors on my part) gets you the id, title, and blurb of the arc, ordered by the latest story in each arc.
In Rails, this is something like
Arcs.find(:all, :include => :stories)(There are additional arguments to do the group-by and order-by which practically amount to custom SQL.)
no subject
Date: 2008-07-17 03:51 am (UTC)no subject
Date: 2008-07-17 09:48 am (UTC)no subject
Date: 2008-07-17 02:32 pm (UTC)Arc.find(:all, :include => :stories, :conditions => {story.series => @series}, :distinct => true)
Also, Rails is picky that models be defined as singular...it goes all weird if it thinks the model is plural and you have to override a lot of the built in methods. I usually get by this by adding another word to the end (so I'd scaffold series as seriesinst, "series instance", instead).
I'm going backwards from how you suggest and learning Rails, CSS and Java through Rails rather than the other way around, though I'm doing it with extremely simple programs at this point.
-Michael
no subject
Date: 2008-07-17 12:20 pm (UTC)