I have three models in an app using edge rails (currently r4833):
class Magazine < ActiveRecord::Base has_many :subscriptions has_many :users, :through => :subscriptions end
class Subscription < ActiveRecord::Base belongs_to :user belongs_to :magazine end
class User < ActiveRecord::Base has_many :subscriptions has_many :magazines, :through => :subscriptions end
I can then create instances and associate them with no problems:
user1 = User.create(:name => "Bob") user2 = User.create(:name => "Fred") magazine = Magazine.create(:name => "WTF?!") magazine.users << user1 magazine.users << user2
magazine.users
=> [#<User:0x2491238 @attributes={"name"=>"Bob", "id"=>1, "subscription_id"=>nil}, @new_record=false, @new_record_before_save=true, @errors=#<ActiveRecord::Errors:0x2427e3c @base=#<User:0x2491238 ...>, @errors={}>>, #<User:0x241dea0 @attributes={"name"=>"Fred", "id"=>2, "subscription_id"=>nil}, @new_record=false, @new_record_before_save=true, @errors=#<ActiveRecord::Errors:0x241c870 @base=#<User:0x241dea0 ...>, @errors={}>>]
Ok, cool, stuff is working so far. Now, though, I want to clear WTF?!'s subscription list:
magazine.users.clear
=>
magazine.save
=> true
This causes the following statement to be run:
Magazine Update (0.001742) UPDATE magazines SET "subscription_id" = NULL, "name" = 'WTF?!' WHERE id = 1
And this seems to have worked:
magazine.subscription_id
=> nil
However, if I reload the model, I suddenly have my old users!
magazine.reload
=> #<Magazine:0x23fd7a4 @subscriptions=nil, @attributes={"name"=>"WTF?!", "id"=>"1", "subscription_id"=>nil}, @users=nil, @errors=#<ActiveRecord::Errors:0x23e20e4 @base=#<Magazine:0x23fd7a4 ...>, @errors={}>, @new_record_before_save=nil>
magazine.users
=> [#<User:0x2389fe8 @attributes={"name"=>"Bob", "id"=>"1", "subscription_id"=>nil}>, #<User:0x2389fac @attributes={"name"=>"Fred", "id"=>"2", "subscription_id"=>nil}>]
magazine.subscription_id
=> nil
Huh? What am I missing? I wouldn't expect nil ids to ever match!
A second question, is there any way to *delete* rows in the join table? Rather than simply nullifying the join table rows, I'd like to kill them completely. Is that possible?