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
On 2/21/06, Tom Taylor <tom@tomtaylor.co.uk> wrote:> 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, > > TomI 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
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
> That''s a shame. It seems perfectly logical to me. Rails 1.2? > > Thanks a lot, > > Tompatch? 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
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.netHi, 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 12/14/06, Steve Midgley <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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 >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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Max Muermann wrote:> On 12/14/06, Steve Midgley <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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, > MaxHi 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---