This has been killing me this weekend. Doing a has_many fetch is accurate the
*first* time only.
Here's a simple example:
tp = TestParent.new(:name => "testing")
tp.save!
tc = TestChild.new(:name => "test1", :test_parent_id => tp.id)
tc.save!
tc = TestChild.new(:name => "test2", :test_parent_id => tp.id)
tc.save!
tc = TestChild.new(:name => "test3", :test_parent_id => tp.id)
tc.save!
tp.test_children.map {|x| x.name }.to_sentence # => "test1, test2, and test3"
*CORRECT*
tc = TestChild.new(:name => "test4", :test_parent_id => tp.id)
tc.save!
tp.test_children.map {|x| x.name }.to_sentence # => "test1, test2, and test3"
*WRONG!*
tp = TestParent.find(tp.id)
tp.test_children.map {|x| x.name }.to_sentence # => "test1, test2, test3, and
test4" *CORRECT*
In my case, we were preparing an order of line items (the children of the
order) and then fetching the results to get a subtotal to add one last line
item (think tax or such) but then the last line item is lost on the following
receipt page, mailers, etc.
This is Rails 4.0.8, Pg 0.17.1, ruby 2.1.1p30 (2014-02-10 revision 44904)
[x86_64-linux]. Models and migration are very, very basic in the test above:
---- migration
class HasManyTest < ActiveRecord::Migration
def self.up
execute %{
CREATE TABLE test_parents (
id serial primary key,
created_at timestamp ,
updated_at timestamp,
name varchar(255)
);
CREATE TABLE test_children (
id serial primary key,
created_at timestamp ,
updated_at timestamp,
name varchar(255),
test_parent_id int4 references test_parents(id)
);
}
end
def self.down
raise "No you can't!"
end
end
----
----
class TestParent < ActiveRecord::Base
has_many :test_children
end
----
----
class TestChild < ActiveRecord::Base
belongs_to :test_parent
end
----
Is this a known problem? It's hard to believe others haven't experienced this?
:') Or is this an obscure result of some sort of caching or such I should be
turning off?
Thanks!
Phil
--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/115BBF21-E488-4180-9A28-2AA833D799C0%40edgedesign.us.
For more options, visit https://groups.google.com/d/optout.