On Tuesday, February 11, 2014 11:59:46 AM UTC-8, Jamie Hodge wrote:
>
> Mode code looks something like this. Surely there is a better way to be 
> doing this, no?
>
> # model
>
> many_to_many :resources, join_table: :items
> one_to_many :items
> ...
>
> # association model
>
> dataset_module do
>   
>   def lock_version
>     select_map(Sequel.qualify(model.table_name, :lock_version))
>   end
> end
>
> # controller
>
> def dataset
>   model.eager(:items, resources: { original: :proxies })
> end
>
> # view
>
> def etag
>   [
>     collection,
>     collection.items_dataset,
>     collection.resources_dataset,
>     collection.originals_dataset,
>     collection.proxies_dataset
>   ].map(&:lock_version).join
> end
>

If you've already eager loaded all the values, it seems pointless to check 
with the database to get the lock version.  In addition to being 
inefficient, it also creates a race condition, in that the etag created 
won't match the eagerly loaded values displayed on the page if there are 
modificaitons between the object retrieval and the etag call.  I'd do 
something like this:

 def etag
    a = [collection]
    a << collection.map(&:items).flatten.uniq
    a << collection.map(&:resources).flatten.uniq
    a << a.last.map(&:original).uniq.compact
    a << a.last.map(&:proxies).flatten.uniq
    a.map(&:lock_version).join
  end

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