Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- model/User.rb
- class User < ActiveRecord::Base
- ...
- has_many :relationships, :foreign_key => "follower_id",
- :dependent => :destroy
- has_many :following, :through => :relationships, :source => :followed
- ...
- end
- model/users_follower.rb
- class UsersFollower < ActiveRecord::Base
- attr_accessible :followed_id
- belongs_to :follower, :class_name => "User"
- belongs_to :followed, :class_name => "User"
- validates :follower_id, :presence => true
- validates :followed_id, :presence => true
- end
- ERROR
- rails c
- Loading development environment (Rails 3.0.3)
- ruby-1.9.2-p136 :001 > user = User.first
- => #<User id: 1, name: "Example User", email: "example@railstutorial.org", created_at: "2011-02-19 21:16:33", updated_at: "2011-02-19 21:16:33", encrypted_password: "b18cdd76431a516f6416babaeaa3ae7f1de652c09efe18b46b3...", salt: "b22ca176d829a222a287b98538d7ad41355e9d5fd2b00d7deee...", admin: true>
- ruby-1.9.2-p136 :002 > user.relationships.create!(:followed_id => 3)
- => #<Relationship follower_id: 1, followed_id: 3, created_at: "2011-02-19 21:34:30", updated_at: "2011-02-19 21:34:30">
- ruby-1.9.2-p136 :003 > user.relationships.find_by_followed_id(3)
- => #<Relationship follower_id: 1, followed_id: 3, created_at: "2011-02-19 21:34:30", updated_at: "2011-02-19 21:34:30">
- ruby-1.9.2-p136 :004 > user.relationships.delete(user.relationships.find_by_followed_id(3))
- NoMethodError: undefined method `eq' for nil:NilClass
- from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/whiny_nil.rb:48:in `method_missing'
- from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/persistence.rb:79:in `destroy'
- from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/locking/optimistic.rb:110:in `destroy'
- from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/callbacks.rb:260:in `block in destroy'
- from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:413:in `_run_destroy_callbacks'
- from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/callbacks.rb:260:in `destroy'
- from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/transactions.rb:232:in `block in destroy'
- from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/transactions.rb:289:in `block in with_transaction_returning_status'
- from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
- from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/transactions.rb:204:in `transaction'
- from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/transactions.rb:287:in `with_transaction_returning_status'
- from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/transactions.rb:232:in `destroy'
- from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/associations/has_many_association.rb:73:in `block in delete_records'
- from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/associations/has_many_association.rb:73:in `each'
- from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/associations/has_many_association.rb:73:in `delete_records'
- from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/associations/association_collection.rb:222:in `block in delete'
- from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/associations/association_collection.rb:525:in `block in remove_records'
- from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/associations/association_collection.rb:158:in `block in transaction'
- from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
- from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/transactions.rb:204:in `transaction'
- from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/associations/association_collection.rb:157:in `transaction'
- from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/associations/association_collection.rb:522:in `remove_records'
- from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/associations/association_collection.rb:221:in `delete'
- from (irb):4
- from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/commands/console.rb:44:in `start'
- from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/commands/console.rb:8:in `start'
- from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/commands.rb:23:in `<top (required)>'
- from script/rails:6:in `require'
- from script/rails:6:in `<main>'ruby-1.9.2-p136 :005 >
- ruby-1.9.2-p136 :006 >
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement