Has_many :through failing to save changes


#1

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


#2

On 2/21/06, Tom T. 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


#3

Rick O. wrote:

On 2/21/06, Tom T. 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


#4

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


#5

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

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


#6

On 12/14/06, Steve M. 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

Have a look if that helps you.

Cheers,
Max


#7

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

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

Cheers
Luke


#8

Max M. wrote:

On 12/14/06, Steve M. 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

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