I suppose that makes sense in a way -- I guess what I am asking for is
the "easy" thing you suggest: treating strict_param_setting as if it's
always false.  At present, there are two ways to do what I want:

1) Do what you said and set strict_param_setting to false for the
model instance, or
2) Do what I'm doing at the moment and add the_hash.delete_if {|k,v| !
['field1', 'field2'].include?(k)} before a my_model.update (not
update_only) call.

In any event, I appreciate the thorough explanation, thanks!

Max

On Mar 8, 1:54 pm, Jeremy Evans <[email protected]> wrote:
> On Mar 8, 11:57 am, Max <[email protected]> wrote:
>
>
>
> > Hi,
>
> > I'd like to draw attention to update_only -- the current
> > implementation of set_restricted creates some unexpected behavior.
>
> > So assume you have a model with attributes id, field1, and field2,
> > with id being the pk.  If you have a hash that's {"id" => 3, "field1"
> > => "foo", "field2" => "bar"}, and you say my_model.update_only
> > (the_hash, "field1"), then you'll get a "method id= doesn't exist or
> > access is restricted to it" if strict is enabled, which appears to be
> > by default.
>
> > The current doc describes update_only this way: "Update the values
> > using the entries in the hash, only if the key is included in only."
>
> > Does this make any sense?  If I say "only update these fields" then
> > presumably it should only be updating those fields, right?  It doesn't
> > seem logical to me that I say "only update these fields" and then it
> > throws an error trying to update a field I didn't specify.
>
> This wasn't really planned, it's more of an implementation detail, but
> it is consistent when you consider how the other set/update methods
> behave.  You can use "my_model.strict_param_setting = false" (or a
> similar change on a per model or global basis) currently to do what
> you want.  I agree that the RDoc should be more descriptive.
>
> Before I consider changing the behavior, their should be a discussion
> of what the semantics should be.  Currently, the semantics are (set|
> update)_only are:
>
> 1) The allowed setter methods are set to only the columns given.
> 2) For each key in the hash, attempt to call the setter method.
> 3) If the setter method isn't in the list of allowed setter methods,
> raise an error unless strict_param_setting is false.
>
> Are you suggesting set_only be treated as if that strict_param_setting
> is always false?  That's not hard to do, but I don't like the lack of
> consistency.  If you are suggesting that setter methods for the
> columns given be called regardless of what is in the hash, that's more
> difficult, as both strings and symbols are allowed as keys (the hash
> could be converted to all strings first, but I don't like that idea).
>
> In the next version of Sequel, this will be very easy to do globally
> via a plugin such as:
>
>   module Sequel
>     module Plugins
>       module SetOnlyNotStrict
>         module InstanceMethods
>           def set_only(*args)
>             old = strict_param_setting
>             self.strict_param_setting = false
>             begin
>               super
>             ensure
>               self.strict_param_setting = false
>             end
>           end
>         end
>       end
>     end
>   end
>   Model.plugin :set_only_not_strict
>
> Or you will be able to override it directly in Model:
>
>   class Sequel::Model
>     def set_only(*args)
>       old = strict_param_setting
>       self.strict_param_setting = false
>       begin
>         super
>       ensure
>         self.strict_param_setting = false
>       end
>     end
>   end
>
> You can't do either in 2.11, unless you apply the plugin/define the
> method in all of the subclasses.
>
> Jeremy
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to