This project is archived and is in readonly mode.
Pluralized AR models (think Posts.find(:all) instead of Post.find(:all))
Reported by Yaroslav Markin | December 31st, 2008 @ 03:25 PM | in 2.x
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.
Comments and changes to this ticket
-
Frederick Cheung December 31st, 2008 @ 03:44 PM
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)
-
DHH December 31st, 2008 @ 03:46 PM
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 ;)
-
Josh Susser December 31st, 2008 @ 03:46 PM
-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.
-
Yaroslav Markin December 31st, 2008 @ 04:01 PM
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.
-
Josh Susser December 31st, 2008 @ 04:17 PM
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.
-
Frederick Cheung December 31st, 2008 @ 04:18 PM
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.
-
Xavier Noria January 2nd, 2009 @ 02:19 AM
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 :-).
-
Yaroslav Markin January 2nd, 2009 @ 10:23 AM
The patch is probably going down, still I tried to solve issues that Frederick pointed to.
-
Yaroslav Markin January 2nd, 2009 @ 10:23 AM
The patch is probably going down, still I tried to solve issues that Frederick pointed to.
-
DHH January 2nd, 2009 @ 10:25 AM
- 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.
-
Oleg Andreev January 2nd, 2009 @ 04:00 PM
Huh. Another, cleaner attempt is here:
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.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>