I would like to be able to quash exceptions and just examine model errors. 
I can almost do this when using the nested attributes plugin due to the way 
it avoids validating on save and instead uses an 
after_validation_hook/after_save_hook (but this has problems for (eg 
automatic) validations that check foreign key presence on new parent 
records).

I share the backward compatibility concern but I would prefer the behaviour 
of the core option to follow the principle of least surprise.  I regard the 
current behaviour as somewhat surprising. This could be fixed with 
documentation and a plugin similar to some of what nested attributes does, 
but it does feel a bit like adding a patch plugin to get correct intent.

Perhaps the middle ground is to do it in core using a hook like 
nested_attributes does so that model errors can be accumulated and examined 
on an error, and allowing a special raise_on_failure/raise_on_save_failure 
value of :none/:never as well as preserving the current falsey/truthy value 
behaviour.

Ultimately I don't mind how we get the behaviour, I'd just like to avoid 
raising exceptions and examine model.errors as this works nicely when 
implementing JSON API's or handling complex form errors in web server code.


On Monday, 13 January 2014 08:20:52 UTC+10, Jeremy Evans wrote:
>
> On Sunday, January 12, 2014 11:03:26 AM UTC-8, Christoph Haas wrote:
>>
>> Evening… 
>>
>> I'm on Sequel 4.6.0 and want to use it to build a web application with 
>> Padrino and test it with RSpec. I did not find any public project of 
>> that combination that allowed me to look how others work with it so I'm 
>> advancing slowly with 20 tabs of documentation open in my browser. :) 
>>
>> At the moment I'm writing tests for my models. And it seems that if I'm 
>> trying to creating an associated record I get this exception: 
>>
>> ->  Sequel::Error: invalid associated object, cannot save 
>>
>> although I have set "raise_on_save_failure=false" which indeed works 
>> when I'm trying to save invalid model records. 
>>
>> This is my model: 
>>
>> class VirtualDomain < Sequel::Model 
>>   one_to_many :virtual_users, :key => 'domain_id' 
>>
>>   plugin :validation_helpers 
>>   def validate 
>>     …some validation 
>>   end 
>> end 
>>
>> class VirtualUser < Sequel::Model 
>>   many_to_one :virtual_domain, :key => 'domain_id' 
>>
>>   plugin :validation_helpers 
>>   def validate 
>>     …some validation 
>>   end 
>> end 
>>
>> I have set this before connecting to my MySQL database: 
>>
>> Sequel::Model.raise_on_save_failure = false 
>>
>> Now what I'm trying in my tests (or on the console): 
>>
>> > domain=VirtualDomain.create(:name=>'example.com') 
>> > domain.add_virtual_user(:email=>'invalid@address', 
>> :password=>'tooshort') 
>> Sequel::Error: invalid associated object, cannot save 
>>
>> [For some reason I can't even catch that exception from RSpec like 
>> expect(…).to raise_error] 
>>
>> A valid workaround seems to be: 
>>
>> > domain=VirtualDomain.create(:name=>'example.com') 
>> > user=VirtualUser.new(:email=>'invalid@address', :password=>'tooshort') 
>> > expect(user).to be_valid 
>>
>> I have basically no idea of the Sequel core itself. But it seems to me 
>> like the truth might be somewhere here: 
>>
>> https://github.com/jeremyevans/sequel/blob/dde03831d556570f9c60826f6fdf304febd2e339/lib/sequel/model/associations.rb#L1445
>>  
>> Perhaps a simple condition check if raise_on_save_failure is set before 
>> raising that error? 
>>
>> Any hints?
>>
>
> Your workaround of creating it manually is the correct way to handle it 
> currently. In terms of handling it inside Sequel, I don't think it should 
> be changed by default, as the backwards compatibility breakage is too high. 
>  I suppose we could handle it via an additional plugin.  What do other 
> users think of this situation?
>
> Thanks,
> Jeremy
>

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to