On Wednesday, May 18, 2016 at 5:56:22 AM UTC-7, Tiago Cardoso wrote:
>
> There was a very handy method in activerecord: calling #size on 
> associations
>
> This would work like: if the association had been previously loaded, it 
> would call #size on the array. If not, it would perform an SQL COUNT. 
>
> As previously discussed, associations are by default arrays. Does the 
> association proxy plugin cover this case?
>

No.  If you want something like it without association proxies:

associated = obj.associations[:association] 
associated ? associated.size : obj.association_dataset.count

With the association_proxies plugin, you'd have to use the count method, 
since that method is implemented by both Array and Sequel::Dataset, 
something like this (untested):

plugin :association_proxies do |opts|
  if opts[:method] == :count
    !opts[:instance].associations[opts[:reflection][:name]]
  else
    ![].respond_to?(opts[:method])
  end
end

It's certainly possible to expand the association_proxies plugin to allow 
for customization of the proxy class, but currently that isn't possible on 
a per-association basis.  It is possible to modify the 
Sequel::Plugins::AssociationProxies::AssociationProxy class to add custom 
behavior that will be shared by all associations using association_proxies, 
though.

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