私はtwitterのシステムと同様のシステムを開発しています。だから私はこの問題の3つの授業、すなわち投稿、ユーザー、および言及を扱っています。参照オブジェクトは、記述されたフィールド(上記のユーザ)とpost_id(それが起こるポスト)を持っています。 テストを書くときに奇妙な問題が見つかったので、コンソールセッションを開きました。これが起こります。 2人のユーザー、2人の投稿、2つのコメントを作成します。私はちょうどuser2.idの代わりに、@を使用する場合は、@ user2が、不思議なことに特定のフィールドのIDが間違っているオブジェクトを作成するレール
ruby-1.9.2-p180 :001 > @user1 = Factory(:user, :email => Factory.next(:email))
=> #<User id: 1, name: "Fulano", email: "[email protected]", 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: "[email protected]", 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">
:あなたが気付いた場合、私は二言及を作成するときに、(@ mention2)は@mentioned_idのIDが正しいユーザーのIDではありませんその最後の行はすべて正常に動作します:
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">
誰でも何が起こっているか教えてください。私はレールを "マジック"がオブジェクトを参照するときにIDを取得するのを世話したと思った。そして、なぜそれは最初のユーザーでは動作しますが、2番目のユーザーでは動作しませんか?
これは私の言及クラスのコードです:
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
最後の行は、.idメソッドを使用すると正しく動作しますが、前の2つは正しく動作します。 ..
これまでに試してみたところ、うまくいきませんでした。私がこれを行うと、何らかの理由で、言及されたreferences_idフィールドがnilに設定されます。 –
ruby-1.9.2-p180:005> p.mentions.create:mention => user1 =># –
コンソールで 'p.mentions.create:mention => user1'エントリを実行すると、おそらく"警告:保護されていない属性:述べた "。これはあなたの 'mention'モデルの' attr_accessible:mention_id'行のためです。それを取り除くとうまくいきます。 –