This project is archived and is in readonly mode.
has_one :through not working?
-
Altair
I've been having the same problem with my models. Is this correct behavior?
It's quite annoying to have to set associations after the original model has been saved. I understand why this might be necessary, but I also believe that the :through association should be taking care of updating the record after the original model is saved.
Another time that this becomes a problem is in HABTM relationships that specify a :join_model.
-
Altair
Correction, HABTM still works in a test case I wrote up. It must be an application peculiarity of my own.
Still, has_one :through remains a problem.
-
Jacob Atzen
Basically this is just the way Rails works. What you want to do is probably something like:
u = User.new u.build_profile u.profile.build_contact u.save -
Altair
I misread the original ticket, but there is, however, still a problem with has_one :through. Note that this issue is non-existent in a has_many :through.
Suppose that you have the same schema as outlined above. Executing the following results in incorrect behavior:
>> Profile.all => [] >> u = User.create => #<User id: 1, created_at: "2009-03-15 18:08:52", updated_at: "2009-03-15 18:08:52"> >> c = Contact.new => #<Contact id: nil, created_at: nil, updated_at: nil> >> c.user = u => #<User id: 1, created_at: "2009-03-15 18:08:52", updated_at: "2009-03-15 18:08:52"> >> Profile.all => [#<Profile id: 1, user_id: 1, contact_id: nil, created_at: "2009-03-15 18:09:21", updated_at: "2009-03-15 18:09:21">] >> c.save => true >> c.user => nil >> Profile.all => [#<Profile id: 1, user_id: 1, contact_id: nil, created_at: "2009-03-15 18:09:21", updated_at: "2009-03-15 18:09:21">]What's happening here is that when you assign a :user to a :contact that has not yet been saved, an entry in :profile is created before the :contact even has an id (hence the nil for contact_id). With a has_many :through association, this is not an issue; entries in the association we are traversing :through are created only on the save. Clearly, the association we are going :through should not be creating records before a save on the parent model.
-
visnu
I ran into this same problem. has_one :through isn't honoring the contract of a normal has_one association. from the current documentation:
Assigning an object to a has_one association automatically saves that object and the object being replaced (if there is one), in order to update their primary keys - except if the parent object is unsaved (new_record? == true).
I've attached a test and a working patch.
-
visnu
- Tag changed from :through, has_one to :through, has_one, patch
-
visnu
can anyone verify or give me feedback on my patch?
-
visnu
what else can I do to draw more attention to this issue?
-
Altair
I haven't had a chance to apply your patch; I've moved on to other things.
Unfortunately I don't really think you can do much to draw more attention to the issue. The thing is that there are a couple ways you can work around the problem. What I ended up doing just as a short-term solution was using a has_many, and just enforcing that the "many" was only one.
You could try directly emailing some of the core developers, but I imagine they get all sorts of email every day, and you'll probably get lost in the shuffle. My guess is that this might have been caught already anyway, and will probably be fixed at least by the time 3.0 is released.
That all said, I'll see if I can scrounge up some time to take a look at testing your patch. After all, you did put the effort in. Which is appreciated, by the way.
-
Eloy Duran
- Assigned user set to Michael Koziarski
- Milestone changed from 2.x to 2.3.4
I think Michael is the person for this one.
And indeed, tickets with fixes like these will definitely be taken care of. It's just hard to put a date on it. Thanks.
-
Wolfram Arnold
Has anyone tries setting :autosave => true? Would this fix it? I haven't tried this but from reading the issue this is what I'd try.
-
visnu
- Tag changed from :through, has_one, patch to :through, bugmash, has_one, patch
sneaking this into the bugmash spotlight
-
Rizwan Reza
verified
+1 This patch works in 2-3-stable, all tests pass. Does not work in master.
-
Greg Sterndale
+1 verified
has_one_through_for_new_records.diff patch applied and tests pass for 2-3-stable
patch fails to apply to master
-
Elad Meidar
+1 verified, +1 patch fixes on 2-3-stable with tests passing
on master, same issue and patch fails.
Loading development environment (Rails 3.0.pre) >> Profile.all => [] >> u = User.create => #<User id: 1, created_at: "2009-08-09 23:08:47", updated_at: "2009-08-09 23:08:47"> >> c = Contact.new => #<Contact id: nil, created_at: nil, updated_at: nil> >> c.user = u => #<User id: 1, created_at: "2009-08-09 23:08:47", updated_at: "2009-08-09 23:08:47"> >> Profile.all => [#<Profile id: 1, user_id: 1, contact_id: nil, created_at: "2009-08-09 23:08:57", updated_at: "2009-08-09 23:08:57">] >> c.save => true >> c.user => nil >> Profile.all => [#<Profile id: 1, user_id: 1, contact_id: nil, created_at: "2009-08-09 23:08:57", updated_at: "2009-08-09 23:08:57">] -
Pratik
We need a patch for the master branch too.
-
Tristan Dunn
I verified the failure on master and I've attached a patch for master.
-
Elad Meidar
+1 Verified patch on master, applies cleanly and tests run
Loading development environment (Rails 3.0.pre) >> Profile.all => [] >> u = User.create => #<User id: 1, created_at: "2009-08-09 23:41:33", updated_at: "2009-08-09 23:41:33"> >> c = Contact.new => #<Contact id: nil, created_at: nil, updated_at: nil> >> c.user = u => #<User id: 1, created_at: "2009-08-09 23:41:33", updated_at: "2009-08-09 23:41:33"> >> Profile.all => [] >> c.save => true >> c.user => #<User id: 1, created_at: "2009-08-09 23:41:33", updated_at: "2009-08-09 23:41:48"> >> Profile.all => [#<Profile id: 1, user_id: 1, contact_id: 1, created_at: "2009-08-09 23:41:48", updated_at: "2009-08-09 23:41:48">] -
Repository
- State changed from new to resolved
(from [ecc9b705d7d12f853c794ca564b8e72995563eae]) Allow ho:through#build when the owner is a new record [#1749 state:resolved]
Signed-off-by: Pratik Naik pratiknaik@gmail.com
http://github.com/rails/rails/commit/ecc9b705d7d12f853c794ca564b8e7... -
Repository
(from [a0f69722be00cd546558b067054e9e7ae2564274]) Allow ho:through#build when the owner is a new record [#1749 state:resolved]
Signed-off-by: Pratik Naik pratiknaik@gmail.com
http://github.com/rails/rails/commit/a0f69722be00cd546558b067054e9e... -
CancelProfileIsBroken
- Assigned user cleared.
- Tag changed from :through, bugmash, has_one, patch to :through, has_one, patch
- Milestone cleared.
