Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Pluralized AR models (think Posts.find(:all) instead of Post.find(:all))

#1675

Patch introduces pluralized AR models generation. For instance, if you have


class Post < ActiveRecord::Base
end

you'll automatically get Posts object proxy that will call singular class methods.


Posts.find(:all), Posts.all, Posts.find_by_title, Posts.scoped_by_date

are now available automatically ;-)

Not destructive to existent classes/modules.

AS::Dependency modified a bit so that when trying to lookup Posts constant, it will look for both posts.rb and post.rb.

Would love to hear any feedback.

Reported by Yaroslav Markin · December 31st, 2008 @ 03:25 PM

State: wontfix
Milestone: 2.x
Assigned to: DHH DHH
Importance: none

Activity

  1. Yaroslav Markin
  2. Yaroslav Markin
  3. Frederick Cheung
    Frederick Cheung

    What's the motivation for this? Seems to me like quite a lot of magic just to make some code read more like English (which I think is something of a false idol)

    December 31st, 2008 @ 03:44 PM

  4. DHH
    DHH

    I must admit that I've wanted this since forever, but I'm not sure if we should go there. Let's get some opinions on where it might provide trouble.

    But I still to this day write Posts.all in the console. Which is an indicator to me that my brain doesn't want to concede collection operations on a singular class ;)

    December 31st, 2008 @ 03:46 PM

  5. Josh Susser
    Josh Susser

    -1

    This change needlessly pollutes the constant namespace, and offers no increased functionality. It's nothing but sugar with no nutritional value. And it comes at a potentially high cost of complicating both the class-loading code and muddying a rather simple set of conventions. It's likely also to play havoc with the set of emerging Ruby IDEs and their ability to locate class definitions in files.

    December 31st, 2008 @ 03:46 PM

  6. Yaroslav Markin
    Yaroslav Markin

    Josh, how do you think, is there any way this functionality can be done in a more simple way without complicating stuff? Please share any ideas. Can't really think of one..

    It sure complicates class loading a bit and adds class proxies just for cool syntax, but the ability to call finders on plural class is something I'd love to have.

    December 31st, 2008 @ 04:01 PM

  7. Josh Susser
    Josh Susser

    I don't have any ideas on how to simplify this, but I'm not really motivated to do so. I think this is a bad idea, and enabling sloppy coding doesn't seem like a good reason to add complication.

    December 31st, 2008 @ 04:17 PM

  8. Frederick Cheung
    Frederick Cheung

    Also the code does not quite honour the promise not to clobber an existing Posts class: it does indeed check defined?(Posts) but the posts class may simply not be loaded yet.

    December 31st, 2008 @ 04:18 PM

  9. Yaroslav Markin
    Yaroslav Markin

    Frederick, you are right, thanks for noticing this.

    December 31st, 2008 @ 04:36 PM

  10. Xavier Noria
    Xavier Noria

    Mixed feelings as well.

    Also, I often write Posts.all. That kind of suggests there's something into this feature. But on the other hand models are singular by convention and Post.all is plain Ruby I should have no problem with.

    Smells to me like adding too much stuff to the namespace.

    I am +0 on this one :-).

    January 2nd, 2009 @ 02:19 AM

  11. Yaroslav Markin
    Yaroslav Markin

    The patch is probably going down, still I tried to solve issues that Frederick pointed to.

    January 2nd, 2009 @ 10:23 AM

  12. Yaroslav Markin
    Yaroslav Markin

    The patch is probably going down, still I tried to solve issues that Frederick pointed to.

    January 2nd, 2009 @ 10:23 AM

  13. DHH
    DHH
    • State changed from new to wontfix

    Yeah, I don't see this making it in either. But I want to commend you for the effort :D. You could definitely turn it into a plugin, though.

    January 2nd, 2009 @ 10:25 AM

  14. Yaroslav Markin
  15. Oleg Andreev
    Oleg Andreev

    Huh. Another, cleaner attempt is here:

    http://gist.github.com/42569

    The idea is to return an appropriate singular const when const_missing is called (like "Posts not found"). No constants are created.

    This should work fine with the evil dependencies.rb :-), but i have not tried yet.

    January 2nd, 2009 @ 04:00 PM