i cannot clear relation with rails 3

hi,

im trying to update a relation through has many, but i cannot figure out how to do it, so i thought i can just delete all the relation and create them again, but i get error on clear method?

c = Campaign.last

=> #<Campaign id: 5, name: "sortable", description: "pokads", default_segment: nil, account_id: 4, promotion_created: true>

c.campaigns_segments

=> [#<CampaignsSegments segment_id: 1, campaign_id: 5, sortable: nil>, #<CampaignsSegments segment_id: 5, campaign_id: 5, sortable: nil>]

c.campaigns_segments.clear

NoMethodError: undefined method `in' for nil:NilClass   from /Library/Ruby/Gems/1.8/gems/activesupport-3.0.0/lib/active_support/whiny_nil.rb:48:in `method_missing'   from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/associations/has_many_association.rb:79:in `delete_records'   from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/associations/association_collection.rb:222:in `delete'   from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/associations/association_collection.rb:527:in `remove_records'   from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/associations/association_collection.rb:158:in `transaction'   from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'   from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/transactions.rb:204:in `transaction'   from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/associations/association_collection.rb:157:in `transaction'   from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/associations/association_collection.rb:524:in `remove_records'   from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/associations/association_collection.rb:221:in `delete'   from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/associations/association_collection.rb:167:in `delete_all'   from /Library/Ruby/Gems/1.8/gems/activerecord-3.0.0/lib/active_record/associations/association_collection.rb:248:in `clear'   from (irb):50

Is this rails 3 error or what?

please help :slight_smile:

thanks..

Hope you figured this out six months ago, but I just spent a few minutes on this issue and found the answer after finding this unanswered post. It may not be your solution, but it solved my issue with this exact error.

For simple mapping tables using the :has_many relationship, the mapping table needs to have an autoincrementing primary key, 'id'. If you create the table with :id => false, then you're going to have this problem once you try to start deleting from this table (you can insert and read from it fine).

This is unlike relationships with the :has_and_belong_to_many, which mapping tables do not require (and in fact can not have, I don't think) a PK 'id' field.

Jamal Soueidan wrote in post #957804: