Epiphanies and Weird Ideas

April 30th, 2008

A lot has changed since I started this blog so I think I need to give this a renewed effort. I’m going to try and temper my obsession with getting everything perfect. My previous approach to blogging has resulted in 4 published posts in 9 months and about 40 drafts. What a fucking waste.

So from now on the content will be significantly shittier than it was and far more frequent, like a stomach flu. Here’s to fresh starts.

An unoriginal weird idea

I needed to do something weird in a plugin I wrote. I needed two models to represent the same table. I needed models that act like views. I’ve seen plugins that try to accomplish that goal but they have one of two problems:

  • Muck around with ActiveRecord internal data structures
  • Are pretty much all or nothing (no way of getting at the whole table through a scoped model)

The second issue I could have hacked around.

The first is a show stopper for me. Ruby gives you a ton of power to monkey-patch but, I think those changes should be kept as close to the public API as possible and never ever directly manipulate internal (to what you’re patching) data structures.

One day this may be promoted to “bad idea”

So I wrote my own. I’m not even convinced this is of general interest. So much so that it’s actually part of a larger plugin I wrote, just an implementation detail of that plugin really. That said, if there is a positive response, I’ll wrap it up and spec it out properly. For now, I’m going to go the worst possible route for software distribution, uploading it as an asset for my blog. Classy, huh?

scoped_model.rb

Just a simple ActiveRecord::Base.send(:include, JamLab::ActsAsScopedModel) and you’re off and running.

Usage is exactly like #with_scope

class DumbUsers < ActiveRecord::Base
  acts_as_scoped_model :find => {:conditions => {:is_dumb => true}}
end

Now use the model as usual and the proper scope is used everywhere. It makes absolutely zero attempt to control the creation of records in any way. You can create a record using a scoped model that does contradict the assigned scope.

An important note, I’m not using this in production yet so, this may be riddled with bugs. If you do find any, drop me a line.

9 Responses to “Epiphanies and Weird Ideas”

  1. Travis Vocino Says:
    Yeah, you need to put to use the fucking PR5 link I give you on my homepage, asshole.
  2. Rick Martinez Says:
    Hawt.
  3. Mark Says:
    Usefull post!
  4. Josh Owens Says:
    Useful post? As in, I like to reinvent the wheel useful? In case you haven't heard, they pulled has_finder into core rails for 2.1, which is coming out very shortly. It offers great support for treating your scopes like associations, you can chain them together even. On top of that, it has nice proc support.
  5. Rich Cavanaugh Says:
    Josh, might I suggest a course in reading comprehension? I'm well aware of named_scope in Rails 2.1, it's a great feature and I love it. I explicitly said the use case for this was for completely separate models representing different portions of a table. Kinda like a view[1] at the database level. Care to explain how named_scope applies to that requirement? [1] http://en.wikipedia.org/wiki/View_%28database%29
  6. Josh Owens Says:
    Rich, I suppose you could suggest one, but I would probably be the teacher, given my high marks on test scores, etc. As for your post and your half-assed plugin, you don't explain your use case at all. I am working on similar data extraction to do statistical math and subqueries and views are one possible route to help speed up query time (it was up to 7 seconds just to pull basic average over time stats). The other option is to de-normalize your data. De-normalizing has proven to be faster to implement imo and given the time based nature of my needs, the calculations only need to be calculated nightly. I think this is more the classic case of you trying to over-complicated an issue, just like I saw many a times through various pieces of code shown to me. I just don't get it. As for how to use named_scope to do what you want... Both your "plugin" and has_finder use with_scope to do some magic. I bet you can find a smart way to extend and help an open source project (take your pick, rails or has_finder seperately) and maybe help others in the process? Or just go on writing silly one off solutions.
  7. Rich Cavanaugh Says:

    While still riddled with the level of maturity you bitched about me having at least your comment has some actual interesting meat to it now.

    I was being purposely vague about the use case, I guess that bit me in the form of your comments. The use case for this code is to be part of a much larger plugin, as I said in the post, this code is really just a small detail of that plugin. This was not meant to be a fully polished plugin, just demonstrating an idea.

    In my case de-normalization is not an option. Separate models are a hard requirement. I need everything a model can do, including different associations, validations and callbacks on each model. While named_scope and my code above touch similar areas, the end result is significantly different.

    This is simply an extraction from a larger plugin that is due to be released shortly (we were waiting on Rails 2.1). It is properly packaged as a plugin, with complete tests and documentation.

    It’s called acts_as_revisable and you can take a look at it here. It’s meant as a more flexible implementation of the core ideas from acts_as_versioned. So, I am contributing to the community. It still needs some work and hasn’t been announced just yet but, I thought this may help you see what I’m trying to accomplish.

  8. Travis Vocino Says:

    Posting in a Rich vs. Josh internet fight thread.

  9. RIch Cavanaugh Says:

    Travis, yeah, they’re always fun but I’m trying to move this into a more productive area. I just hope Josh cooperates.

Sorry, comments are closed for this article.