# Has\_many :through failing to save changes

**URL:** <https://www.ruby-forum.com/t/has-many-through-failing-to-save-changes/71392>\
**Category:** Rails\
**Created:** [February 21, 2006, 4:33pm UTC](https://www.ruby-forum.com/t/has-many-through-failing-to-save-changes/71392 "2006-02-21T16:33:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [February 21, 2006, 4:33pm UTC](https://www.ruby-forum.com/t/has-many-through-failing-to-save-changes/71392/1 "2006-02-21T16:33:16Z")

</div>

Hi all,

I’m messing around with has\_many, and has\_many :through (on edge rails),  
using a join model. Branches and Projects join through  
ProjectsAtBranches.

If I do @branch.projects, I can see the associated projects, and  
visa-versa, if I manually put them in the join table.

But I cannot do @branch.projects \<\< Branch.find(blah), or  
@branch.projects.clear, and then save the results. It works fine in  
@branch, until I try to save it, and the child rows never save or alter.  
@branch.reload brings me back to step 1.

class Branch \< ActiveRecord::Base  
has\_many :projects, :through =\> :projects\_at\_branches, :dependent =\>  
true  
has\_many :projects\_at\_branches, :dependent =\> true  
end

class Project \< ActiveRecord::Base  
has\_many :branches, :through =\> :projects\_at\_branches, :dependent =\>  
true  
has\_many :projects\_at\_branches, :dependent =\> true  
end

class ProjectsAtBranch \< ActiveRecord::Base  
belongs\_to :project  
belongs\_to :branch  
end

Any suggestions?

Thanks a lot,

Tom

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [February 21, 2006, 5:13pm UTC](https://www.ruby-forum.com/t/has-many-through-failing-to-save-changes/71392/2 "2006-02-21T17:13:00Z")

</div>

On 2/21/06, Tom T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> @branch, until I try to save it, and the child rows never save or alter.  
> end  
> Tom  
> I don’t think you’re supposed to use the :through associations to  
> modify things. You should probably use the actual assocation  
> :projects\_at\_branches.

–  
Rick O.  
[http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [February 21, 2006, 5:31pm UTC](https://www.ruby-forum.com/t/has-many-through-failing-to-save-changes/71392/3 "2006-02-21T17:31:30Z")

</div>

Rick O. wrote:

> On 2/21/06, Tom T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I’m messing around with has\_many, and has\_many :through (on edge rails),  
> > using a join model. Branches and Projects join through ProjectsAtBranches.

> I don’t think you’re supposed to use the :through associations to  
> modify things. You should probably use the actual assocation  
> :projects\_at\_branches.

That’s a shame. It seems perfectly logical to me. Rails 1.2?

Thanks a lot,

Tom

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [February 21, 2006, 11:03pm UTC](https://www.ruby-forum.com/t/has-many-through-failing-to-save-changes/71392/4 "2006-02-21T23:03:53Z")

</div>

> That’s a shame. It seems perfectly logical to me. Rails 1.2?
> 
> Thanks a lot,
> 
> Tom

patch? ticket?

I’ve been trying to fill in some of the gaps as I run into them,  
especially with polymorphic joins…

–  
Rick O.  
[http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [December 13, 2006, 11:15pm UTC](https://www.ruby-forum.com/t/has-many-through-failing-to-save-changes/71392/5 "2006-12-13T23:15:22Z")

</div>

Rick O. wrote:

> > That’s a shame. It seems perfectly logical to me. Rails 1.2?
> > 
> > Thanks a lot,
> > 
> > Tom
> 
> patch? ticket?
> 
> I’ve been trying to fill in some of the gaps as I run into them,  
> especially with polymorphic joins…
> 
> –  
> Rick O.  
> [http://techno-weenie.net](http://techno-weenie.net)

Hi,

I’m having a similar problem and wonder if there are any updates on  
status since this original email? Is there now a ticket or patch for  
this issue?

Seems like an important tool: to be able to not just navigate but create  
relationships between two related tables. I can see the pain point: how  
to allow additional model data into the join table, if it requires it  
for referential or data integrity…

Any news on this thread would be appreciated.

Steve

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [December 13, 2006, 11:36pm UTC](https://www.ruby-forum.com/t/has-many-through-failing-to-save-changes/71392/6 "2006-12-13T23:36:06Z")

</div>

On 12/14/06, Steve M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > I’ve been trying to fill in some of the gaps as I run into them,  
> > this issue?
> 
> Seems like an important tool: to be able to not just navigate but create  
> relationships between two related tables. I can see the pain point: how  
> to allow additional model data into the join table, if it requires it  
> for referential or data integrity…
> 
> Any news on this thread would be appreciated.
> 
> Steve

Adding to has\_many :through via \<\< works for me in Edge rails. There  
is a problem with self-referential relationships, for which I have  
filed a patch:

[http://dev.rubyonrails.org/ticket/6744](http://dev.rubyonrails.org/ticket/6744)

Have a look if that helps you.

Cheers,  
Max

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [December 14, 2006, 1:11pm UTC](https://www.ruby-forum.com/t/has-many-through-failing-to-save-changes/71392/7 "2006-12-14T13:11:34Z")

</div>

Steve

Couple of articles that might interest you regarding this problem:

Why it isn’t/wasn’t working:  
[http://blog.hasmanythrough.com/articles/2006/04/17/join-models-not-proxy-collections](http://blog.hasmanythrough.com/articles/2006/04/17/join-models-not-proxy-collections)

Changes to edge rails:  
[http://blog.hasmanythrough.com/articles/2006/08/19/magic-join-model-creation](http://blog.hasmanythrough.com/articles/2006/08/19/magic-join-model-creation)

Cheers  
Luke

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [December 14, 2006, 8:43am UTC](https://www.ruby-forum.com/t/has-many-through-failing-to-save-changes/71392/8 "2006-12-14T08:43:52Z")

</div>

Max M. wrote:

> On 12/14/06, Steve M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > I’ve been trying to fill in some of the gaps as I run into them,  
> > > this issue?
> > 
> > Seems like an important tool: to be able to not just navigate but create  
> > relationships between two related tables. I can see the pain point: how  
> > to allow additional model data into the join table, if it requires it  
> > for referential or data integrity…
> > 
> > Any news on this thread would be appreciated.
> > 
> > Steve
> 
> Adding to has\_many :through via \<\< works for me in Edge rails. There  
> is a problem with self-referential relationships, for which I have  
> filed a patch:
> 
> [http://dev.rubyonrails.org/ticket/6744](http://dev.rubyonrails.org/ticket/6744)
> 
> Have a look if that helps you.
> 
> Cheers,  
> Max

Hi Max,

Thanks for this reference - I had noticed your posting about  
self-references while researching this problem, though I wasn’t certain  
it was related.

(I am using 1.1.6 Rails) Just to be clear, when you say that adding two  
models using “has\_many :through” works for you, does this mean that you  
are able to add both models and the model that connects them is  
correctly updated to include a join record as well?

So, assume tables with pseudo-Rails code like:

owner  
id  
name :string  
property  
id  
location : string  
owner\_property (join table)  
id  
owner\_id  
property\_id  
created\_at [etc → i.e. other 5th normal columns)

Property  
has\_many :owner\_property  
has\_many :owner, :through =\> owner\_property

Owner  
has\_many :owner\_property  
has\_many :property, :through =\> owner\_property

OwnerProperty  
has\_many :owner  
has\_many :property

Can you execute this code or equivalent with the appropriate results  
(detailed below)

owner.create!({:name =\> “User 1”})  
property.create!({:location =\> “101 Main St”})  
owner \<\< property  
owner.save!  
property.save! # not sure if either save! statement is even necessary

If working correctly this would yields a new owner record (say id:15), a  
new property record (say id:20) and a new owner\_property record with  
owner\_id = 15 and property\_id = 20 (and created\_at =\> [Now])…

I’ll be very curious if this is working well for some people because I’m  
totally flummoxed on getting it to work in my setup.

Thanks for any input or suggestions. I’m happy to move up to Edge rails  
if it’s necessary but I prefer to work on the stable release because  
that’s what I’ll be (soon) deploying under (am I foolish to do this?)

Sincerely,

Steve
