2013-03-14 96 views
1

我有麻煩建立協會。我有我的模型定義如下:如何建立工廠女孩協會?

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

和我現在我試圖像創建工廠「conversation_with_messages」定義這些工廠

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 

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列需要不爲空。

我想知道爲什麼這個專欄沒有填寫,我在這裏做錯了什麼?

回答

1

您是否在對話模型關係中指定了class_name

class Conversation 
    belongs_to :user1, class_name: 'User' 
    belongs_to :user2, class_name: 'User' 
    has_many :messages 
end 
0

當測試很難時,考慮修改您的設計。兩個想法涌現在腦海裏:

1)MUSTUser■找多個Conversation S'

如果類似Twitter的直接消息模型(任意兩個用戶之間的一個連續的談話)是可以接受的,那麼你可以去的東西,如:

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

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 
+0

因爲某些原因我需要分組對象,將聚集的消息,並測試本身並不難 - 我只是與我的想法是很簡單的例子創建正確FactoryGirl代碼中的問題。 – 2013-03-14 15:08:25