Forum: Ruby on Rails has_many :through failing to save changes

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
143d37bc67b04ad4fdc69beddda48651?d=identicon&s=25 Tom Taylor (Guest)
on 2006-02-21 16:33
(Received via mailing list)
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
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2006-02-21 17:13
(Received via mailing list)
On 2/21/06, Tom Taylor <tom@tomtaylor.co.uk> 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 Olson
http://techno-weenie.net
143d37bc67b04ad4fdc69beddda48651?d=identicon&s=25 Tom Taylor (Guest)
on 2006-02-21 17:31
(Received via mailing list)
Rick Olson wrote:
> On 2/21/06, Tom Taylor <tom@tomtaylor.co.uk> 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
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2006-02-21 23:03
(Received via mailing list)
> 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 Olson
http://techno-weenie.net
B483dea19c00b3d952f4958d67ea4bd1?d=identicon&s=25 Steve Midgley (stevemidgley)
on 2006-12-13 23:15
Rick Olson 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 Olson
> 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
E6a1fe85299e663566dd1ea7d4f74e76?d=identicon&s=25 Max Muermann (Guest)
on 2006-12-13 23:36
(Received via mailing list)
On 12/14/06, Steve Midgley <rails-mailing-list@andreas-s.net> 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

Have a look if that helps you.

Cheers,
Max
B483dea19c00b3d952f4958d67ea4bd1?d=identicon&s=25 Steve Midgley (stevemidgley)
on 2006-12-14 08:43
Max Muermann wrote:
> On 12/14/06, Steve Midgley <rails-mailing-list@andreas-s.net> 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
>
> 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
F2289d673c5f7ebffc045ab74e58bbeb?d=identicon&s=25 Luke (Guest)
on 2006-12-14 13:11
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...

Changes to edge rails:
http://blog.hasmanythrough.com/articles/2006/08/19...

Cheers
Luke
This topic is locked and can not be replied to.