Guest User

Untitled

a guest
Aug 25th, 2018
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. Rails creating object with the wrong id for certain field
  2. ruby-1.9.2-p180 :001 > @user1 = Factory(:user, :email => Factory.next(:email))
  3.  
  4. => #<User id: 1, name: "Fulano", email: "person_number1@example.com", created_at: "2011-09-11 15:38:11", updated_at: "2011-09-11 15:38:11", encrypted_password: "ede4e8cc0440b8bd9fdc059e8496154715b6592690157aac618...", salt: "4bfae8fecee1629b7c290c2d5af5bd3bbeb38c4ce76546d7176...", admin: false>
  5.  
  6. ruby-1.9.2-p180 :002 > @user2 = Factory(:user, :email => Factory.next(:email))
  7.  
  8. => #<User id: 2, name: "Fulano", email: "person_number2@example.com", created_at: "2011-09-11 15:39:06", updated_at: "2011-09-11 15:39:06", encrypted_password: "ddda10f48575f63724e1db3d3cf684f2c60380325236311e15d...", salt: "911121b8942dc57116dfd24383d96874c750bc5a21fa210288b...", admin: false>
  9.  
  10. ruby-1.9.2-p180 :003 > @post1 = Factory(:post, :user => @user1)
  11.  
  12. => #<post id: 1, content: "Foo bar", user_id: 1, created_at: "2011-09-11 15:39:57", updated_at: "2011-09-11 15:39:57">
  13.  
  14. ruby-1.9.2-p180 :004 > @post2 = Factory(:post, :user => @user2)
  15.  
  16. => #<post id: 2, content: "Foo bar", user_id: 2, created_at: "2011-09-11 15:40:37", updated_at: "2011-09-11 15:40:37">
  17.  
  18. ruby-1.9.2-p180 :005 > @mention1 = @post2.mentions.create :mentioned_id => @user1
  19.  
  20. => #<Mention id: 1, post_id: 2, mentioned_id: 1, created_at: "2011-09-11 15:41:33", updated_at: "2011-09-11 15:41:33">
  21.  
  22. ruby-1.9.2-p180 :007 > @mention2 = @post1.mentions.create :mentioned_id => @user2
  23.  
  24. => #<Mention id: 2, post_id: 1, mentioned_id: 1, created_at: "2011-09-11 15:42:16", updated_at: "2011-09-11 15:42:16">
  25.  
  26. ruby-1.9.2-p180 :008 > @mention2 = @post1.mentions.create :mentioned_id => @user2.id
  27.  
  28.  
  29. => #<Mention id: 3, post_id: 1, mentioned_id: 2, created_at: "2011-09-11 15:45:16", updated_at: "2011-09-11 15:45:16">
  30.  
  31. class Mention < ActiveRecord::Base
  32. attr_accessible :mentioned_id
  33.  
  34. belongs_to :mentioned, :class_name => "User"
  35. belongs_to :post
  36.  
  37. validates :mentioned, :presence => true
  38. validates :post, :presence => true
  39.  
  40. end
  41.  
  42. Class User < ActiveRecord::Base
  43.  
  44.  
  45. has_many :posts, :dependent => :destroy
  46. has_many :mentions, :dependent => :destroy, :foreign_key => 'mentioned_id'
  47.  
  48.  
  49. @user = Factory(:user, :email => Factory.next(:email))
  50. @other_user = Factory(:user, :email => Factory.next(:email))
  51.  
  52. @post1 = Factory(:post, :user => @other_user)
  53. @post2 = Factory(:post, :user => @other_user)
  54. @post3 = Factory(:post, :user => @user)
  55. @mention1 = @post1.mentions.create :mentioned_id => @user
  56. @mention2 = @post2.mentions.create :mentioned_id => @user
  57. @mention3 = @post3.mentions.create :mentioned_id => @other_user.id
  58.  
  59. @mention1 = @post1.mentions.create :mentioned => @user
  60.  
  61. attr_accessible :mentioned_id, :mentioned
Add Comment
Please, Sign In to add comment