On Monday, October 22, 2018 at 2:58:11 AM UTC-7, Eugene Lai wrote:
>
> Hi,
>
> Reading through here 
> <https://github.com/jeremyevans/sequel/blob/master/doc/model_hooks.rdoc> 
> the documentation for Model hooks says in several places that hooks will 
> only be called if they are called on a Model and not a Dataset object. It 
> also states that the return from something like
> MyModel.where(something: 'something else')
> is going to be dataset.
>
> I currently have a situation where I have a Dataset that I accidentally 
> called #destroy on, but to my surprise it ran, and to my even greater 
> surprise, it ran the destroy hooks. I went through the documentation here 
> <https://sequel.jeremyevans.net/rdoc/classes/Sequel/Dataset.html> and it 
> seemed to me a Dataset shouldn't even have #destroy, only #delete.
>
> Here is my code:
>
> class CompanyEventFile < Sequel::Model
>
>   def after_destroy
>     super
>     # do something...
>   end
>
> end
>
> class CompanyEvent < Sequel::Model
>
>   def event_files
>     CompantEventFile.where(event_id: id)
>   end
>
> end
>
> CompanyEvent[1].event_files.destroy # this runs, and triggers the 
> hook...why?
> CompanyEvent[1].event_files.class # => Sequel::Postgres::Dataset
> CompanyEvent[1].event_files.method(:destroy).source_location # => 
> ["/usr/local/lib/ruby/gems/2.4.0/gems/sequel-5.10.0/lib/sequel/model/base.rb",
>  
> 2054]
>

Dataset#destroy on model datasets exists for backwards compatibility. It 
retrieves all objects in the dataset, then calls Model#destroy on each.  So 
this is expected behavior.

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 https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to