Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Rails creating object with the wrong id for certain field
- ruby-1.9.2-p180 :001 > @user1 = Factory(:user, :email => Factory.next(:email))
- => #<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>
- ruby-1.9.2-p180 :002 > @user2 = Factory(:user, :email => Factory.next(:email))
- => #<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>
- ruby-1.9.2-p180 :003 > @post1 = Factory(:post, :user => @user1)
- => #<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">
- ruby-1.9.2-p180 :004 > @post2 = Factory(:post, :user => @user2)
- => #<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">
- ruby-1.9.2-p180 :005 > @mention1 = @post2.mentions.create :mentioned_id => @user1
- => #<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">
- ruby-1.9.2-p180 :007 > @mention2 = @post1.mentions.create :mentioned_id => @user2
- => #<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">
- ruby-1.9.2-p180 :008 > @mention2 = @post1.mentions.create :mentioned_id => @user2.id
- => #<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">
- class Mention < ActiveRecord::Base
- attr_accessible :mentioned_id
- belongs_to :mentioned, :class_name => "User"
- belongs_to :post
- validates :mentioned, :presence => true
- validates :post, :presence => true
- end
- Class User < ActiveRecord::Base
- …
- has_many :posts, :dependent => :destroy
- has_many :mentions, :dependent => :destroy, :foreign_key => 'mentioned_id'
- …
- @user = Factory(:user, :email => Factory.next(:email))
- @other_user = Factory(:user, :email => Factory.next(:email))
- @post1 = Factory(:post, :user => @other_user)
- @post2 = Factory(:post, :user => @other_user)
- @post3 = Factory(:post, :user => @user)
- @mention1 = @post1.mentions.create :mentioned_id => @user
- @mention2 = @post2.mentions.create :mentioned_id => @user
- @mention3 = @post3.mentions.create :mentioned_id => @other_user.id
- @mention1 = @post1.mentions.create :mentioned => @user
- attr_accessible :mentioned_id, :mentioned
Add Comment
Please, Sign In to add comment