On Wed, Feb 16, 2022 at 6:14 AM cra...@gmail.com <crai...@gmail.com> wrote:
> I'm trying to define a subset in a Model: > > class User < Sequel::Model > subset :active, Sequel.~(:inactive) > # proc > subset :name, -> (name) { where( Sequel.like(:username, > "%#{params[:name]}%") ) } > end > > When I attempt to use the model, I get an error: > > `<class:User>': undefined method `subset' for User:Class (NoMethodError) > > What am I not understanding? > That the method does not exist. Since Sequel 5, subset is only defined inside dataset_module blocks. If you want to use it at class level, you need to use the def_dataset_method plugin. > Are procs allowed? > Not in the way you are using them. You want to do: class User < Sequel::Model dataset_module do subset :active, Sequel.~(:inactive) def name(name) # Note that this probably won't work anyway, since params is not defined. # You probably want to use name instead of params[:name], and call this # method with params[:name] where( Sequel.like(:username, "%#{params[:name]}%") ) end end end The block you pass to subset ends up being passed to the Dataset#where method, but it isn't passed the arguments you are passing to where. 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 sequel-talk+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sequel-talk/CADGZSSekCAd8juduBbhAY5UaqVEKOCaQuMBJ0rds_2F6DmfCMw%40mail.gmail.com.