1

アソシエーションの構築に問題があります。私は自分のモデルを次のように定義しています:

class Conversation
  belongs_to :user1
  belongs_to :user2
  has_many :messages
end

これらの工場を定義しました

factory :user do
  name "name"
end

factory :female, parent: :user do
  gender 'f'
end 

factory :male, parent: :user do
  gender 'm'
end 

factory :message do
  message "message"
  conversation
end

今、私はこのような工場「conversation_with_messages」を作成しようとしています

factory :conversation do
    read false
    association :user1, factory: :male
    association :user2, factory: :female    
    factory :conversation_with_messages do

      ignore do
        messages_count 10
      end

      after(:create) do |conversation, evaluator|
        FactoryGirl.create_list(:message, evaluator.messages_count, author: conversation.user1)
      end
    end
  end

しかしFactoryGirl.create(:conversation_with_messages)、そうすると、user1_id列がnullではない必要があるというデータベースエラーが発生します。

このコラムが記入されていない理由と、ここで何が間違っているのか疑問に思っています。

4

2 に答える 2

1

会話モデルの関係でを指定しclass_nameましたか?

class Conversation
  belongs_to :user1, class_name: 'User'
  belongs_to :user2, class_name: 'User'
  has_many :messages
end
于 2013-03-14T14:06:34.623 に答える
0

テストが困難な場合は、設計の変更を検討してください。次の 2 つの考えが思い浮かびます。

1) s には複数の s が必要です か?UserConversation

Twitter のダイレクト メッセージ モデル (任意の 2 人のユーザー間の 1 つの継続的な会話) のようなものが許容される場合は、次のようなものを使用できます。

class Message < ActiveRecord::Base
  belongs_to :sender, class_name: 'User'
  belongs_to :recipient, class_name: 'User'

  default_scope order("created_at DESC")

  def read?
    !self.unread?
  end

  def read_or_unread
    self.unread? ? "unread" : "read"
  end
end

class User < ActiveRecord::Base
  has_many :messages, foreign_key: :recipient_id

  def messages_grouped_by_sender
    msg_ids = messages.select("MAX(id) AS id").group(:sender_id).collect(&:id)
    Message.includes(:sender).where(id: msg_ids)
  end
end

class Conversation

  THEM_TO_ME = "sender_id = :their_id AND recipient_id = :my_id"
  ME_TO_THEM = "sender_id = :my_id AND recipient_id = :their_id"

  def initialize(me, them)
    @me = me
    @them = them
  end

  def them
    @them
  end

  def thread
    Message.where("#{ME_TO_THEM} OR #{THEM_TO_ME}", ids)
  end

  def unread?
    # Checking only the newest message is good enough
    messages_to_me.first.try(:unread)
  end

  def mark_as_read
    messages_to_me.where(:unread => true).update_all(:unread => false)
  end

  def to_or_from_me(message)
    message.sender == @me ? "From" : "To"
  end

  private

  def messages_to_me
    Message.where(THEM_TO_ME, ids)
  end

  def ids
    { :my_id => @me.id, :their_id => @them.id }
  end
end

2)Conversationデータベースに永続化する必要がありますか?

Message次のように見える場合はConversation、メッセージを取得してから前のメッセージのチェーンをたどって a を初期化できます。

class Message < ActiveRecord::Base
  belongs_to :sender, class_name: 'User'
  belongs_to :recipient, class_name: 'User'
  belongs_to :previous_message, class_name: 'Message'
end

class Conversation
  def initialize(message)
    @message = message
  end

  def messages
    //use @message to follow the chain of messages
  end
end
于 2013-03-14T14:43:10.220 に答える