Announcing image_associations
Discussions of multi-model forms and nested models in Rails has been revived recently, with various changes appearing in Edge Rails, plugins like attribute_fu getting a lot of attention, and the release of ActivePresenter. It looks like when the dust settles we’ll have a nice new set of ways to simplify our code.
One thing I frequently find myself doing is associating multiple images with a given model. My news story might have a banner image, and a series of other attachments, which I could specify with:
class Story < ActiveRecord::Base belongs_to :banner_image has_many :story_attachments end class BannerImage < ActiveRecord::Base has_attachment # I'm using attachment_fu end class StoryAttachment < ActiveRecord::Base has_attachment # I'm using attachment_fu end
(or I could use has_one in place of belongs_to there, your tastes/requirements may vary)
What quickly becomes a pain is assigning the images to the models, and having rejected fat controllers I often end up writing accessors on my models to manage that for me:
class Story < ActiveRecord::Base belongs_to :banner_image has_many :story_attachments def banner_image(data) if valid_file?(data) self.banner_image = BannerImage.create(:uploaded_data => data) end end def valid_file? # etc. end end
That quickly gets dull, so I've wrapped it up in a plugin I'm calling image_associations. With that I can write:
class Story < ActiveRecord::Base belongs_to_image :banner_image has_many_images :story_attachments end
and get the accessors for free.
It may only be of use to me, and it's pretty crude (there's no support for deleting attachments, for example) but it's been working nicely and has DRYed up my code in a satisfying way, so I thought I'd throw it out there. You can find it over on github.
(I'm not promising to keep supporting it, but it is used in an active project so chances are bug fixes will make it in. And of course, anyone is welcome to branch the project and twist it in their own ways)
Delayed Job dying silently
about 1 month ago - No comments
I’ve just completed migrating a client site from BackgrounDRb to delayed_job (which is a huge relief on several levels). I had hoped to complete the process this morning, but the delayed_job process kept dying on me without any apparent explanation in the usual logs. Thankfully the RPM log in my app turned out to be More >
Character encodings, Rails 3 and Ruby 1.9.2
about 3 months ago - No comments
In a lengthy blog post detailing many of the intricacies and some of the politics relating to character encodings in Ruby, Yehuda Katz has a few paragraphs that left me more than a little excited: The most common scenario where you can see this issue is when the user pastes in content from Microsoft Word, More >
has_many_polymorphs and Rails 3
about 3 months ago - 2 comments
I’m gradually porting a number of my older Rails apps over to Rails 3. The main motivation is a chance to really put the new version through its paces, get a better sense of how it’s working, where plugins are at, etc; but it’s also rather nice to get some of the performance improvements and More >
Asset bundling in Rails
about 3 months ago - No comments
I stumbled across James Herdman’s piece on asset bundling in rails earlier this week. I’d always presumed you could do this but never got round to investigating as very few of my projects load in very large numbers of JS/CSS assets. In the end it was quite timely as I’ve been trying to reduce the More >
Faster Rails development with bundler and rvm
about 4 months ago - 2 comments
If you’re anything like me, you’ve found the rails server and console taking longer and longer to launch lately. Even switching to Rails 3 for most active projects hasn’t really helped. But I finally found a solution a couple of weeks ago. I found it reading Mikel Lindsaar’s Bundle Me Some Sanity where he outlines More >
More notes from a Rails 3.0pre upgrade
about 8 months ago - Comments Off
This is a follow-on from my piece on how I got the (development version of) Catapult Magazine up and running with Rails 3.0pre. If you haven’t already done so, I’d recommend you read that first. Catapult makes use of the permalink_fu plugin which fails in Rails 3. It fails because of a reliance on the More >
Upgrading an app to Rails 3.0pre
about 8 months ago - 1 comment
I used to be a strong adherent to tracking edge rails. Up until the release of rails 2.3 I let most of my frequently updated projects track edge with a vendored copy of rails, and it rarely caused me any trouble. When 2.3 hit I rethought all that. With Rails 3 development ramping up I More >
Cucumber, wordpress and database_cleaner
about 1 year ago - Comments Off
I’m up to my usual using-ruby-tools-to-test-other-environments tricks, using cucumber and my wordpress activerecord classes to do acceptance testing against a highly-customised wordpress install. I’m hoping to write a bit more about that soon, once I’ve put it through its paces a little more and cleaned up some of the code, but I wanted to quickly More >
Talking to WordPress with ActiveRecord
about 1 year ago - Comments Off
As mentioned in yesterday’s announcement I’m pulling some content across from this blog (running on wordpress) into the new Ket Lai site (a merb app). I’ve found myself doing similar things a few times lately, such as on Only Connect (on which more, soon) and so have built out a selection of ActiveRecord models to More >
Selected (belated, extended) Saturday Links
about 1 year ago - Comments Off
The past two weeks haven’t really left time to compile my selected links, though there have been many. A few days at SxSWi (on which more, later) followed by travelling with the family and the inevitable work backlog moved blogging way down the priority list. So here’s a mammoth selection to get me caught up. More >
Comments are closed.
about 2 years ago
I wouldn’t mind adding this to attachment_fu with method names like belongs_to_attachment and has_many_attachments… This was actually how attachment_fu worked originally.
about 2 years ago
That’d be great, rick.
I’m headed out of town tomorrow but could look at what it’d take to turn my plugin into a patch some time next week?
about 2 years ago
Have you looked at the rewrite branch of attachment_fu at all? I’ve been wanting to make it master and push the current master to a legacy branch. It’s a *huge* change from the current attachment_fu though. But IMO it’s a much-needed simplification and reorg of the code.
about 2 years ago
I haven’t had a chance, but will definitely check it out and explore next week.