Brian Sheehan
2007-Aug-01 12:39 UTC
[Betternestedset-talk] Reason why should #parent_id=(x) raise an exception?
Hi all, Can someone explain to me the reasoning behind the current implementation of the attribute writer for the parent_id column in a nested set object? At the moment it just throws an error: def #{acts_as_nested_set_options[:parent_column]}=(x) raise ActiveRecord::ActiveRecordError, "Unauthorized assignment to #{acts_as_nested_set_options[:parent_column]}: it''s an internal field handled by acts_as_nested_set code, use move_to_* methods instead." end Would it not have been possible to define it as something like this: def #{acts_as_nested_set_options[:parent_column]}=(x) move_to_child_of(x) end ? If the above could work, it would allow you to do bulk assignment to the parent_id attribute safely, which would save typing a few lines in controller code... but maybe I''m missing something! thanks, Brian
Jean-Christophe Michel
2007-Aug-01 21:30 UTC
[Betternestedset-talk] Reason why should #parent_id=(x) raise an exception?
Hi, Le 1 ao?t 07 ? 14:39, Brian Sheehan a ?crit :> Can someone explain to me the reasoning behind the current > implementation of the attribute writer for the parent_id column in a > nested set object? At the moment it just throws an error: > > def #{acts_as_nested_set_options[:parent_column]}=(x) > raise ActiveRecord::ActiveRecordError, "Unauthorized assignment to > #{acts_as_nested_set_options[:parent_column]}: it''s an internal > field handled by acts_as_nested_set code, use move_to_* methods > instead." > end > > > Would it not have been possible to define it as something like this: > > def #{acts_as_nested_set_options[:parent_column]}=(x) > move_to_child_of(x) > endThere are two reasons: 1. we don''t know if it should be the first or the last child of the new parent 2. more importantly, setting a value in AR objects in rails never makes an automatic save of the object instance. Here move_to_child changes not only db values for the current object, but for many others. That''s why we think it better not to have a simple method to achieve this change. But feel free to alter this behaviour in your copy if you think it''s safe for you ! Jean-Christophe Michel -- symetrie.com Better Nested Set for rails: http://opensource.symetrie.com/trac/better_nested_set
Brian Sheehan
2007-Aug-01 22:12 UTC
[Betternestedset-talk] Reason why should #parent_id=(x) raise an exception?
Hi Jean-Christophe, That makes sense, thank you! Brian On 8/1/07, Jean-Christophe Michel <jc.michel at symetrie.com> wrote:> Hi, > > Le 1 ao?t 07 ? 14:39, Brian Sheehan a ?crit : > > Can someone explain to me the reasoning behind the current > > implementation of the attribute writer for the parent_id column in a > > nested set object? At the moment it just throws an error: > > > > def #{acts_as_nested_set_options[:parent_column]}=(x) > > raise ActiveRecord::ActiveRecordError, "Unauthorized assignment to > > #{acts_as_nested_set_options[:parent_column]}: it''s an internal > > field handled by acts_as_nested_set code, use move_to_* methods > > instead." > > end > > > > > > Would it not have been possible to define it as something like this: > > > > def #{acts_as_nested_set_options[:parent_column]}=(x) > > move_to_child_of(x) > > end > > There are two reasons: > 1. we don''t know if it should be the first or the last child of the > new parent > 2. more importantly, setting a value in AR objects in rails never > makes an automatic save of the object instance. Here move_to_child > changes not only db values for the current object, but for many > others. That''s why we think it better not to have a simple method to > achieve this change. > But feel free to alter this behaviour in your copy if you think it''s > safe for you ! > > Jean-Christophe Michel > -- > symetrie.com > > Better Nested Set for rails: > http://opensource.symetrie.com/trac/better_nested_set > > > _______________________________________________ > Betternestedset-talk mailing list > Betternestedset-talk at rubyforge.org > http://rubyforge.org/mailman/listinfo/betternestedset-talk >