tc.name = "something different"
tc.save!
tp.test_children.map {|x| x.name }.to_sentence # => "test1, test2, test3, and test4" *WRONG*
tp.test_children(true).map {|x| x.name }.to_sentence # => "test1, test2, test3, and something different" *CORRECT*
Another workaround is to just stop using has_many and such in favor of manual functions, a la:
def test_children
return TestChild.where(["test_parent_id = ?", self.id])
end
Is there a way to turn this sort of caching off globally? (Other caching is fine, I don't want to turn all caching off.)
BTW- It is a bit mind blowing that this is turned on by default. Possible data corruption shouldn't ever be preferred by default over (possible) speed gains. I'd still categorize this as a serious bug, at least as a configuration default.
Thanks!
Phil
Is there a way to turn this sort of caching off globally? (Other caching is fine, I don't want to turn all caching off.)
BTW- It is a bit mind blowing that this is turned on by default. Possible data corruption shouldn't ever be preferred by default over (possible) speed gains. I'd still categorize this as a serious bug, at least as a configuration default.
Rails has been like this at least since the 1.0 days - I can't say I've ever run into particular issues from it.
While it can be 'rare' that somebody might run into this, I'd still argue that significant performance gains of caching associations like this is also rare/minimal (would be interesting to test). I'd rather lean towards data accuracy than speed, at least in my case.
I'd simply like a simple config option to turn on/off this feature. I'd also suggest that it could even be dangerous to have on by default if things have to be done in particular ways to get around the potential pit falls, but I'd just be happy to have a config option to turn it off.
Thanks for the reply!
Phil
On Monday, August 18, 2014 5:25:58 AM UTC-7, Jim wrote:It's not a systemic integrity problem, it is the way Rails has always worked. Using tp.test_children.create() is not "another work-around", it is the recommended way of adding children to a parent model that you have already instantiated and has been available for as long as I can remember (at least since rails 2.0.x).Scenarios where you would actually need to re-query the database *every time you access a relation* are rare. If you really need to, you have that option, but in no way should that be a default.Jim
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/319cce88-0041-4a95-9062-1e0cdd80373d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
I guess it's hard to say what the advantages are without testing... Assuming you are already indexing all of your id and *_id database columns and SQL query caching is on, I'm not sure what advantages association caching is actually providing. A test might be to edit collection_association.rb from the gem manually to set force_refresh = true and do some benchmarking (which I'd like to do when I get some time).
Ironically, 37 Signals has some very good articles on how to do caching properly without worrying about getting stale data, this is a basic example:
I'm not sure what you mean by the 'rails way' in this case. Usually the rails way means not having to worry about cache management by default.
In my case I have a mailer that simply looks like this:
def customer_receipt(order)
In the view it does @order.line_items and they are sometimes wrong. This just feels like an ugly hack to try to make rails faster during the days when ruby was really, really, slow (thankfully the latest since 2.0.0 is MUCH faster).
On Tuesday, August 19, 2014 9:01:36 PM UTC-4, Phil wrote:I guess it's hard to say what the advantages are without testing... Assuming you are already indexing all of your id and *_id database columns and SQL query caching is on, I'm not sure what advantages association caching is actually providing. A test might be to edit collection_association.rb from the gem manually to set force_refresh = true and do some benchmarking (which I'd like to do when I get some time).If your dataset is small and properly indexed, and you have very few related items, chances are you will see very little change in performance from reloading every time you access the order line items. If you have several hundred line items, things may be a little different. If your database is on a different machine than the web server, things may also be a little different.
Ironically, 37 Signals has some very good articles on how to do caching properly without worrying about getting stale data, this is a basic example:Ironically, this has nothing to do with what you are having problems with. You will still have the "stale" object. (Unless you are suggesting that Rails should check the updated_at for every ActiveRecord object every single time you access it. In that case, good luck. I doubt you will ever find any ORM so poorly written.)
I'm not sure what you mean by the 'rails way' in this case. Usually the rails way means not having to worry about cache management by default.I don't worry about cache management by default. In addition, I don't have to worry about how to start using the database caching, and when I might need it.In my case I have a mailer that simply looks like this:
def customer_receipt(order)
In the view it does @order.line_items and they are sometimes wrong. This just feels like an ugly hack to try to make rails faster during the days when ruby was really, really, slow (thankfully the latest since 2.0.0 is MUCH faster).If your order line items are wrong, that is because somewhere in the code, somebody made some incorrect assumptions and wrote some incorrect code, examples of which have already been pointed out. Thankfully, the goal of the framework is *not* to make sure that can never happen by default, with the side effect that you may need to make drastic changes to the code if you ever want to start caching. The framework does provide multiple ways to create related many records in a way that updates the relation in the parent object.
In your case, you can either find that problem and fix it, or simply force reload the items in the mailer you are working on. To complain about bad code as a "Rails Problem" is not useful. Bad code can be written in any language, using any framework.
To be honest, I have written code that requires a reload where certain functionality is invoked. I have a callback on deleting a related many that modifies a value in the parent. However, that code really should be refactored such that the children are not destroyed directly, but instead a method on the parent should destroy the child.
Before blaming 'incorrect code' or bad programmers, look back again at my example. It's quite trivial and reproducible. Saves order line items, in what I would consider a completely sane and logical way, call back line items to compute tax, add the last line item (the tax), and then throws the @order to payment processing, mailers, receipt page... where totals are wrong and tax is missing. I understand that doing things differently avoid the problem and I have used them to fix this issue on my end, but I still am quite skeptical as to how a programmer is supposed to know about these pitfalls.