Hello, Rookie here ... learning rails and enjoying it ... having a
bit-o-trouble with has many in the following scenario:

class Party < ActiveRecord::Base
has_many :target_associations, :foreign_key => 'source_party_id',
:class_name => 'PartyAssociation',
:dependent => :destroy

has_many :source_associations, :foreign_key => 'target_party_id',
:class_name => 'PartyAssociation',
:dependent => :destroy
end

class PartyAssociation < ActiveRecord::Base
belongs_to :party, :foreign_key => "source_party_id"
belongs_to :party, :foreign_key => "target_party_id"
end

class CreateParties < ActiveRecord::Migration
def change
create_table :parties do |t|
t.timestamps
end
end
end

class CreatePartyAssociations < ActiveRecord::Migration
def change
create_table :party_associations, :id => false do |t|
t.integer :source_party_id
t.integer :target_party_id

t.timestamps
end
end
end

This RSpec statement:
it "should destroy source associations" do
@party.destroy
end

Causes this ERROR MESSAGE:
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column:
party_associations.: DELETE FROM "party_associations" WHERE
"party_associations"."" = ?

What am I doing wrong that prevents active record from knowing how to
form the delete statement?

--
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@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Colin Law at Jan 14, 2012 at 9:28 am

    On 14 January 2012 01:43, Douglas B. wrote:
    Hello, Rookie here ... learning rails and enjoying it ... having a
    bit-o-trouble with has many in the following scenario:

    class Party < ActiveRecord::Base
    has_many :target_associations, :foreign_key => 'source_party_id',
    :class_name => 'PartyAssociation',
    :dependent => :destroy

    has_many :source_associations, :foreign_key => 'target_party_id',
    :class_name => 'PartyAssociation',
    :dependent => :destroy
    end

    class PartyAssociation < ActiveRecord::Base
    belongs_to :party, :foreign_key => "source_party_id"
    belongs_to :party, :foreign_key => "target_party_id"
    end

    class CreateParties < ActiveRecord::Migration
    def change
    create_table :parties do |t|
    t.timestamps
    end
    end
    end

    class CreatePartyAssociations < ActiveRecord::Migration
    def change
    create_table :party_associations, :id => false do |t|
    You should not have :id false, the table needs an id column. It is
    generally only when you use has_and_belongs_to_many that you do not
    require an id column

    Colin
    t.integer :source_party_id
    t.integer :target_party_id

    t.timestamps
    end
    end
    end

    This RSpec statement:
    it "should destroy source associations" do
    @party.destroy
    end

    Causes this ERROR MESSAGE:
    ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column:
    party_associations.: DELETE FROM "party_associations" WHERE
    "party_associations"."" = ?

    What am I doing wrong that prevents active record from knowing how to
    form the delete statement?
    --
    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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Douglas Birch at Jan 14, 2012 at 6:14 pm
    Uh ... ended up changing things to use the Has Many Through association
    ... now it all works and looks like this:


    #Created a parties table
    class CreateParties < ActiveRecord::Migration
    def change
    create_table :parties do |t|
    t.string :type

    t.timestamps
    end
    end
    end

    #created a join table to associate one party to another
    class CreateAssociatedParties < ActiveRecord::Migration

    def change
    create_table :associated_parties do |t|
    t.integer :association_type_id
    t.integer :parent_id
    t.integer :child_id
    end
    end

    end

    #With a Party class like this using hmt
    class Party < ActiveRecord::Base

    has_many :primary_associations, :foreign_key => 'parent_id',
    :class_name => 'AssociatedParty',
    :dependent => :destroy
    has_many :children, :through => :primary_associations

    has_many :secondary_associations, :foreign_key => 'child_id',
    :class_name => 'AssociatedParty',
    :dependent => :destroy
    has_many :parents, :through => :secondary_associations

    end

    #And the join class like this
    class AssociatedParty < ActiveRecord::Base

    belongs_to :parent, :class_name => "Party"
    belongs_to :child, :class_name => "Party"

    end

    --
    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@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedJan 14, '12 at 1:45a
activeJan 14, '12 at 6:14p
posts3
users2
websiterubyonrails.org
irc#RubyOnRails

2 users in discussion

Douglas Birch: 2 posts Colin Law: 1 post

People

Translate

site design / logo © 2022 Grokbase