Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 5.09 KB | None | 0 0
  1. model/User.rb
  2.  
  3. class User < ActiveRecord::Base
  4. ...
  5.   has_many :relationships, :foreign_key => "follower_id",
  6.                              :dependent => :destroy
  7.   has_many :following, :through => :relationships, :source => :followed
  8.  
  9. ...
  10. end
  11.  
  12. model/users_follower.rb
  13.  
  14. class UsersFollower < ActiveRecord::Base
  15.   attr_accessible :followed_id
  16.  
  17.   belongs_to :follower, :class_name => "User"
  18.   belongs_to :followed, :class_name => "User"
  19.  
  20.   validates :follower_id, :presence => true
  21.   validates :followed_id, :presence => true
  22.  
  23. end
  24.  
  25.  
  26.  
  27. ERROR
  28.  
  29. rails c
  30. Loading development environment (Rails 3.0.3)
  31. ruby-1.9.2-p136 :001 > user = User.first
  32.  => #<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>
  33. ruby-1.9.2-p136 :002 > user.relationships.create!(:followed_id => 3)
  34.  => #<Relationship follower_id: 1, followed_id: 3, created_at: "2011-02-19 21:34:30", updated_at: "2011-02-19 21:34:30">
  35. ruby-1.9.2-p136 :003 > user.relationships.find_by_followed_id(3)
  36.  => #<Relationship follower_id: 1, followed_id: 3, created_at: "2011-02-19 21:34:30", updated_at: "2011-02-19 21:34:30">
  37. ruby-1.9.2-p136 :004 > user.relationships.delete(user.relationships.find_by_followed_id(3))
  38. NoMethodError: undefined method `eq' for nil:NilClass
  39.     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'
  40.     from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/persistence.rb:79:in `destroy'
  41.     from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/locking/optimistic.rb:110:in `destroy'
  42.     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'
  43.     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'
  44.     from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/callbacks.rb:260:in `destroy'
  45.     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'
  46.     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'
  47.     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'
  48.     from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/transactions.rb:204:in `transaction'
  49.     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'
  50.     from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/transactions.rb:232:in `destroy'
  51.     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'
  52.     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'
  53.     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'
  54.     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'
  55.     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'
  56.     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'
  57.     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'
  58.     from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/activerecord-3.0.3/lib/active_record/transactions.rb:204:in `transaction'
  59.     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'
  60.     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'
  61.     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'
  62.     from (irb):4
  63.     from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/commands/console.rb:44:in `start'
  64.     from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/commands/console.rb:8:in `start'
  65.     from /home/dbranco/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/commands.rb:23:in `<top (required)>'
  66.     from script/rails:6:in `require'
  67.     from script/rails:6:in `<main>'ruby-1.9.2-p136 :005 >
  68. ruby-1.9.2-p136 :006 >
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement