Ruby on Rails
Tuesday, January 3, 2012
On Tue, Jan 3, 2012 at 11:55 PM, Linus Pettersson <linus.pettersson@gmail.com> 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
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 --
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment