Nic Williams
2006-Aug-08 10:33 UTC
[ann] Super DRY models - Auto-associations and validations
I''ve written an ActiveRecord extension that allows Associations to be automatically generated at runtime without requiring "has_many" and "belongs_to" to be defined in the class. The use of has_many vs has_one is determined by the pluralisation of the method call. Additionally, it uses the :null => false attribute of columns to provide validates_presence_of automatically to models too. If any of this code is relevant for Core Rails, let me know and I''ll extract it from the gem and create a patch for core. The gem has received a good reception on the Ruby + RoR forums. http://drnicwilliams.com/2006/08/07/ann-dr-nics-magic-models/ Cheers Nic -- Dr Nic Williams http://www.drnicwilliams.com - Ruby/Rails blog skype: nicwilliams (m) +31 62 494 8552 (p) +61 7 3305 7522 (finds me anywhere in the world) (f) +61 7 3305 7572 (sends fax to my email) _______________________________________________ Rails-core mailing list Rails-core@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails-core
Dr. Ernie Prabhakar
2006-Aug-14 15:52 UTC
Re: [ann] Super DRY models - Auto-associations and validations
Hi Nic, I for one think this is incredibly cool, and would love to see it in Rails 1.2 (as part of core)... Thanks! -- Ernie P. On Aug 8, 2006, at 3:33 AM, Nic Williams wrote:> I''ve written an ActiveRecord extension that allows Associations to > be automatically generated at runtime without requiring "has_many" > and "belongs_to" to be defined in the class. The use of has_many vs > has_one is determined by the pluralisation of the method call. > > Additionally, it uses the :null => false attribute of columns to > provide validates_presence_of automatically to models too. > > If any of this code is relevant for Core Rails, let me know and > I''ll extract it from the gem and create a patch for core. The gem > has received a good reception on the Ruby + RoR forums. > > http://drnicwilliams.com/2006/08/07/ann-dr-nics-magic-models/ > > Cheers > Nic > > -- > Dr Nic Williams > http://www.drnicwilliams.com - Ruby/Rails blog > skype: nicwilliams > (m) +31 62 494 8552 > (p) +61 7 3305 7522 (finds me anywhere in the world) > (f) +61 7 3305 7572 (sends fax to my email) > > _______________________________________________ > Rails-core mailing list > Rails-core@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-core_______________________________________________ Rails-core mailing list Rails-core@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails-core
Andrew Kaspick
2006-Aug-14 17:30 UTC
Re: [ann] Super DRY models - Auto-associations and validations
I would agree that this is a nice way to get started with your models, but once you need to do anything beyond the basics, like association extensions for example, even simply renaming the association, then you''ll have to define the association anyway. Nice idea, but I''d rather see this remain as a plugin instead of users getting used to the fact that you don''t have to define model associations or provide validations. If all this did was generate the base model with the associations and validations then that would definitely relieve some initial development time and possibily worthy of core inclusion. Andrew On 8/14/06, Dr. Ernie Prabhakar <drernie@opendarwin.org> wrote:> > Hi Nic, > > I for one think this is incredibly cool, and would love to see it in Rails > 1.2 (as part of core)... > > Thanks! > -- Ernie P. > > On Aug 8, 2006, at 3:33 AM, Nic Williams wrote: > I''ve written an ActiveRecord extension that allows Associations to be > automatically generated at runtime without requiring "has_many" and > "belongs_to" to be defined in the class. The use of has_many vs has_one is > determined by the pluralisation of the method call. > > Additionally, it uses the :null => false attribute of columns to provide > validates_presence_of automatically to models too. > > If any of this code is relevant for Core Rails, let me know and I''ll extract > it from the gem and create a patch for core. The gem has received a good > reception on the Ruby + RoR forums. > > http://drnicwilliams.com/2006/08/07/ann-dr-nics-magic-models/ > > Cheers > Nic > > -- > Dr Nic Williams > http://www.drnicwilliams.com - Ruby/Rails blog > skype: nicwilliams > (m) +31 62 494 8552 > (p) +61 7 3305 7522 (finds me anywhere in the world) > (f) +61 7 3305 7572 (sends fax to my email) > _______________________________________________ > Rails-core mailing list > Rails-core@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-core > > _______________________________________________ > Rails-core mailing list > Rails-core@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-core > > >
Rodrigo Kochenburger
2006-Aug-14 17:39 UTC
Re: [ann] Super DRY models - Auto-associations and validations
I like this, and i believe most people would. I can''t say for the core team, but i think you sould create a patch and post it on trac. The automatically validation can be improved to more than just validates_presence_of as well, others people might help with that and then it would be easier to apply the patch on the trunk ;) Nice job, Nic. Cheers! Rodrigo Kochenburger On 8/14/06, Dr. Ernie Prabhakar <drernie@opendarwin.org> wrote:> > Hi Nic, > > I for one think this is incredibly cool, and would love to see it in Rails > 1.2 (as part of core)... > > Thanks! > -- Ernie P. > > On Aug 8, 2006, at 3:33 AM, Nic Williams wrote: > > I''ve written an ActiveRecord extension that allows Associations to be > automatically generated at runtime without requiring "has_many" and > "belongs_to" to be defined in the class. The use of has_many vs has_one is > determined by the pluralisation of the method call. > > Additionally, it uses the :null => false attribute of columns to provide > validates_presence_of automatically to models too. > > If any of this code is relevant for Core Rails, let me know and I''ll extract > it from the gem and create a patch for core. The gem has received a good > reception on the Ruby + RoR forums. > > http://drnicwilliams.com/2006/08/07/ann-dr-nics-magic-models/ > > Cheers > Nic > > -- > Dr Nic Williams > http://www.drnicwilliams.com - Ruby/Rails blog > skype: nicwilliams > (m) +31 62 494 8552 > (p) +61 7 3305 7522 (finds me anywhere in the world) > (f) +61 7 3305 7572 (sends fax to my email) > > _______________________________________________ > Rails-core mailing list > Rails-core@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-core > > _______________________________________________ > Rails-core mailing list > Rails-core@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-core > > >-- Rodrigo Kochenburger <divoxx at gmail dot com> Linkedin professional profile: http://www.linkedin.com/in/rodrigok
David Heinemeier Hansson
2006-Aug-14 22:42 UTC
Re: [ann] Super DRY models - Auto-associations and validations
> I''ve written an ActiveRecord extension that allows Associations to be > automatically generated at runtime without requiring "has_many" and > "belongs_to" to be defined in the class. The use of has_many vs has_one is > determined by the pluralisation of the method call.Very cool stuff, Nic. I think it would be a great app/plugin to use against databases that follow Rails conventions but that does not necessarily have a Rails application already. I don''t think its a good fit for core, though. Primarily because it splinters the overview. I don''t have any models in any of my real applications where the automatically defined rules would be sufficient. And when they''re not, it means that my validates_format_of will have to be defined explicitly while the validates_presence_of is magic. Or that my has_many :comments, :order => "created_at DESC" has to be explicit, but my has_one :author is automatic. But again, please do keep pushing on with this. Just because its not a good fit for core, doesn''t mean it ain''t cool. Rock on. -- David Heinemeier Hansson http://www.loudthinking.com -- Broadcasting Brain http://www.basecamphq.com -- Online project management http://www.backpackit.com -- Personal information manager http://www.rubyonrails.com -- Web-application framework
Kyle Maxwell
2006-Aug-15 02:09 UTC
Re: Re: [ann] Super DRY models - Auto-associations and validations
> If all this did was generate the base model with the associations and > validations then that would definitely relieve some initial > development time and possibily worthy of core inclusion.+1 for addition to script/generate model/scaffold/etc.
choonkeat
2006-Aug-15 11:05 UTC
Re: Re: [ann] Super DRY models - Auto-associations and validations
would be to use via a more standalone script like script/console (e.g. script/sql ) where I can connect to any db and immediately get models and benefits of AR... then perhaps I really won''t have to enter the mysql prompt anymore. On 8/15/06, Kyle Maxwell <kyle@kylemaxwell.com> wrote:> > > If all this did was generate the base model with the associations and > > validations then that would definitely relieve some initial > > development time and possibily worthy of core inclusion. > > +1 for addition to script/generate model/scaffold/etc. > _______________________________________________ > Rails-core mailing list > Rails-core@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails-core >_______________________________________________ Rails-core mailing list Rails-core@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails-core
Martin Emde
2006-Aug-15 13:13 UTC
Re: [ann] Super DRY models - Auto-associations and validations
On 8/14/06, Andrew Kaspick <akaspick@gmail.com> wrote:> > > If all this did was generate the base model with the associations and > validations then that would definitely relieve some initial > development time and possibily worthy of core inclusion. >It can''t really be included with script/generate model since it bases what it would generate on the migration that would not be created yet. Some sort of script you could run after the model was created and the migrations were done that would add the code would be pretty nice tho. Maybe even something that you could run multiple times that would (and this might be hard) not step on your toes if you had already modified the model. Martin _______________________________________________ Rails-core mailing list Rails-core@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails-core
Nic Williams
2006-Aug-15 17:06 UTC
Re: [ann] Super DRY models - Auto-associations and validations
@Andrew and Kyle: I''ve started a magic_model_generator that will be released after a foreign key support patch. That''ll be a wonderful tool. Not very magical, but useful... non-magically. @DHH and Andrew : I agree with that - in a normal app development, you''ll always want a model class and explicit associations - eventually. Its probably a style preference for agile developers as to whether they are happy to immediately start work with derived associations (like the derived attr_accessors for fields in ARs now) instead of explicitly defining them. "Opinions" are great; so is choice. :) Over time, we''ll start to get feedback on people''s development philosophies/strategies, for new Rails apps, Rails apps on legacy DBs, and non-Rails apps, etc. @Rodrigo - Apparently post-Railsconf (read, post-Dave Thomas talk), some work was done on auto-validations by some people which is more magical that the simple validations that are available now; so I''ll include those too; e.g. validates_uniqueness_of if its a sole-index, etc. Wonderful stuff! Cheers Nic -- Dr Nic Williams http://www.drnicwilliams.com - Ruby/Rails blog skype: nicwilliams (m) +31 62 494 8552 (p) +61 7 3102 3237 (finds me anywhere in the world) (f) +61 7 3305 7572 (sends fax to my email) _______________________________________________ Rails-core mailing list Rails-core@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails-core
David Heinemeier Hansson
2006-Aug-15 17:44 UTC
Re: Re: [ann] Super DRY models - Auto-associations and validations
> Its probably a style preference for agile developers as to whether they are > happy to immediately start work with derived associations (like the derived > attr_accessors for fields in ARs now) instead of explicitly defining them. > "Opinions" are great; so is choice. :)I agree. With Rails, the split is that opinions go into core and options go into plugins. -- David Heinemeier Hansson http://www.loudthinking.com -- Broadcasting Brain http://www.basecamphq.com -- Online project management http://www.backpackit.com -- Personal information manager http://www.rubyonrails.com -- Web-application framework