Carl Lerche
2007-Feb-04 11:15 UTC
Per activerecord object callbacks (aka: should I have gone to bed hours ago?)
First of all, I should have gone to bed a long time, so please be forgiving with regards to what will follow :) So I''ve been working all day on a rails project that needs to go out soon, and I thought I would try to refactor a has_many relationship to a has_and_belongs_to_many relationship (using has_many :through). However, I then realized that I would loose a lot of the simple beauty of my code because has_many :through doesn''t support the create method. I was messing around with this, and I thought, can''t we add a per-object callback system as well as a per class callback system. Then we could add create methods to has_many :through. Here''s my super hacked "proof of concept" (if it can even be called that). http://pastie.caboo.se/37802 and a little extract from the console demonstrating it.>> t = Taggable.create=> #<Taggable:0x1462de0 @attributes={"name"=>nil, "id"=>1}, @new_record_before_save=true, @errors=#<ActiveRecord::Errors:0x2810d2c @base=#<Taggable:0x1462de0 ...>, @errors={}>, @callbacks={}, @new_record=false>>> tag = t.tags.create=> #<Tag:0x27d5b50 @attributes={"name"=>nil}, @errors=#<ActiveRecord::Errors:0x27bd5f0 @base=#<Tag:0x27d5b50 ...>, @errors={"name"=>["can''t be blank"]}>, @callbacks={:after_save=>#<Proc:0x02639800@./script/../config/../config/../lib/am_i_insane.rb:28>}, @new_record=true>>> tag.name = "Hello"=> "Hello">> Tagging.count=> 0>> tag.save=> true>> Tagging.count=> 1 Any thoughts on this? Am I missing some implications or anything? I definitely not am an expert of the ruby on rails code... alright, I''m off to bed. -carl -- EPA Rating: 3000 Lines of Code / Gallon (of coffee) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Julian Tarkhanov
2007-Feb-04 11:58 UTC
Re: Per activerecord object callbacks (aka: should I have gone to bed hours ago?)
On Feb 4, 2007, at 12:15 PM, Carl Lerche wrote:> I was messing around with this, and I thought, can''t we add a > per-object callback system as well as a per class callback system. > Then we could add create methods to has_many :through.Whi don''t you just define create on the association has_many ... do def create ... end end -- Julian ''Julik'' Tarkhanov please send all personal mail to me at julik.nl --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---
Carl Lerche
2007-Feb-04 18:36 UTC
Re: Per activerecord object callbacks (aka: should I have gone to bed hours ago?)
On 2/4/07, Julian Tarkhanov <listbox@julik.nl> wrote:> > > On Feb 4, 2007, at 12:15 PM, Carl Lerche wrote: > > > I was messing around with this, and I thought, can''t we add a > > per-object callback system as well as a per class callback system. > > Then we could add create methods to has_many :through. > > Whi don''t you just define create on the association > > has_many ... do > def create > ... > end > endI''m not exactly sure how defining create on the association would work for a has_many :through association. There is the join model that has to be created. Currently, there is a create! method defined for has_many :through associations. The create! method will throw an error if validations fail. This is necessary because currently, the join object has to be created right away. However, if per-object callbacks are implemented, create is possible... so is build. I don''t know if that makes sense. I will try to work up some better code. -carl -- EPA Rating: 3000 Lines of Code / Gallon (of coffee) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com To unsubscribe from this group, send email to rubyonrails-core-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en -~----------~----~----~----~------~----~------~--~---