Fabien Franzen
2005-Oct-31 10:30 UTC
Proposal: habtm :join_model option to treat join_table as a real model, enables acts_as_*
Hi people, In my pursuit of a working acts_as_list for HasAndBelongsTo associations I would like to propose the following: Currently HABTM uses raw sql to create and delete entries in the join table. This way the normal AR flow is circumvented, including all the normal callback functions (before_create, after_destroy ...) On the other hand, if you could specify that the join table should be treated like a real model (AR object) when doing create and delete on it, it would automatically enable a lot of options. Among them, working acts_as_* functionality, since most of them are dependent on callbacks. So if the :join_model option would be specified, it would create the intermediate join model: has_and_belongs_to_many :images, :join_model => true this would inflect the right model name automatically has_and_belongs_to_many :images, :join_model => ''ImageLookup'' this would be a custom lookup object Otherwise the normal raw sql version kicks in, since that would be the default/fastest implementation. This would effectively solve among others, this ticket: http:// dev.rubyonrails.com/ticket/2025 Unfortunately I''m new to Rails/Ruby, but maybe someone could shed a light on this. Feedback is appreciated. Also: http://dev.rubyonrails.com/ticket/2588 Thanks! - Fabien
Jeremy Kemper
2005-Oct-31 17:13 UTC
Re: Proposal: habtm :join_model option to treat join_table as a real model, enables acts_as_*
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Oct 31, 2005, at 2:30 AM, Fabien Franzen wrote:> In my pursuit of a working acts_as_list for HasAndBelongsTo > associations I would like to propose the following: > > Currently HABTM uses raw sql to create and delete entries in the > join table. This way the normal AR flow is circumvented, including > all the normal callback functions (before_create, after_destroy ...) > > On the other hand, if you could specify that the join table should > be treated like a real model (AR object) when doing create and > delete on it, it would automatically enable a lot of options. Among > them, working acts_as_* functionality, since most of them are > dependent on callbacks.Great thinking, Fabien. In fact, this is the future of HABTM post-1.0. Simplify, jeremy -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2 (Darwin) iD8DBQFDZlDKAQHALep9HFYRAtuhAKDDmgQJCjArpemOxztGT69JscmECgCgpdUw qBe+GasHUBxjwKBGySAaCxs=vXd6 -----END PGP SIGNATURE-----
Atelier Fabien
2005-Oct-31 21:29 UTC
Re: Proposal: habtm :join_model option to treat join_table as a real model, enables acts_as_*
> In fact, this is the future of HABTM post-1.0.I hope this will make it soon! I''ve tried to implement some of it today. But it didn''t work out, when I added: has_and_belongs_to_many(options) ... unless options[:join_model].nil? ... eval("before_destroy { |record| options[:join_model].destroy_all (<condition here>) } else <regular before_destroy RAW SQL: DELETE FROM ...> end This is from the top of my head, since I deleted my changes out of frustration... When doing anything destroy related now, it gives a MethodUndefined error. My guess is that it causes an infinite loop, which gets aborted before it will loop forever. If I change destroy_all to delete_all, this works fine, but off coarse, this doesn''t help a lot since this will circumvent the callbacks too. Also, I''m new to Ruby (and Rails), but I do have a lot of experience in similar model structures and associations. Anyway, I''m looking forward to contributing ideas, and eventually patches, when I master this some more. Regards, Fabien