On Tue, Jan 3, 2012 at 11:55 PM, Linus Pettersson <
[email protected]> wrote:

> Thank you Peter. I have some follow up questions.
>
> Let's say I would use a scope. How could I do that? (let's ignore the
> "image_small" for now and focus on the subcategory) There is nothing in the
> product table that I can use to select the completed products. It is only
> regarded as completed if the related resellercategory is associated to a
> subcategory. So, in the scope I would need to join the resellercategories
> table and check if that is associated with a subcategory.
>

Well, this article

  http://ablogaboutcode.com/2011/02/19/scopes-in-rails-3/

seems to indicate it can be done quite easily

 class User
  scope :by_age, lambda do |age|
    joins(:profile).where('profile.age = ?', age) unless age.nil?
  end
  scope :by_name, lambda{ |name| where(name: name) unless name.nil? }
  scope :by_email, lambda do |email|
    joins(:profile).where('profile.email = ?', email) unless email.nil?
...

It joins user.profile to check on profile.age or profile.email ...

I have not tested it, but seems not too difficult.


> Feels a bit unnecessary to do that for a small thing as this. But maybe it
> won't affect the performance that much.
>

In my view, it is relevant, since it reduces the risk of cache
inconsistency, which
is a much harder problem to solve when your application grows. But I have
had
discussions with colleagues on this before and some actually preferred
caching
such implicit caching columns in local tables, to have simpler reporting on
those
tables later on (but the risk on inconsistency was always there).


>
> The other option I was thinking of was just to add an after_filter in
> resellercategory model like this:
>
>   def update_products
>     self.products.each(&:save)
>   end
>

Yes, this will work.

But for any implicit caching option (also the update_all), you need to know
all the models
where the implicit product.complete cache is affected and have this cache
update code
there (currently that would only be the ResellerCategory model, but it can
grow).

The 3 options will work, but all have pros and cons:
* Product :complete scope (no implicit caching)
* Product update_all (sql level)
* ResellerCategory after_save update_products (ActiveRecord level, but many
queries)

HTH,

Peter

-- 
Peter Vandenabeele
http://twitter.com/peter_v
http://rails.vandenabeele.com

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to