2014-02-17 86 views
1

我有兩個模型 - 用戶和關鍵字,以及第三個模型關聯,用戶和關鍵字通過has_many關係連接。通過關係通過has_many保存記錄

我在關鍵字控制器創建方法,該方法如下所示 -

def create 
    @keyword = Keyword.new(keyword_params) 
    if Keyword.find_by(content: params[:content]).nil? 
    @keyword.save 
    end 

    @keyword.associations.create(:user_id => current_user.id) 
    flash[:success] = "Keyword successfully created!" 
    redirect_to keywords_path 

在上面提到的,用戶添加的關鍵詞後,我檢查如果該關鍵字在已經存在的「創造」方法關鍵字表,如果不存在,則將關鍵字保存在關鍵字表中,然後保存關聯表中用戶和關鍵字之間的關聯。

但是,當一個關鍵字已經存在於關鍵字表中(因爲它可能已經被另一個用戶添加了),並且可以說一個新用戶將這個現有關鍵字添加到他的列表中,它給了我一個錯誤 - 「你由於@ keyword.save被跳過(因爲關鍵字已經存在於數據庫中),所以不能調用create,除非父鍵被保存在@ keyword.associations.create行。

我使用我的新到Rails的Rails 4和Ruby 2.0.0

,並希望得到任何幫助你們可以提供。

更新: 添加關鍵字模式和關鍵詞控制器的細節

型號: 用戶模式:

class User < ActiveRecord::Base 
     before_save { self.email = email.downcase } 
     before_create :create_remember_token 

     has_many :associations 
     has_many :keywords, :through => :associations 

     #name 
     validates :name, presence: true, length: { maximum: 50 } 
    end 

關鍵字模式:

class Keyword < ActiveRecord::Base 
    has_many :questions 
    has_many :associations 
    has_many :users, :through => :associations 
    validates :content, presence: true, uniqueness: { case_sensitive: false } 
end 

關聯模型

class Association < ActiveRecord::Base 
     belongs_to :keyword 
     belongs_to :user 
     validates :user_id, :uniqueness => { :scope => :keyword_id } 
    end 

關鍵詞控制器:

class KeywordsController < ApplicationController 
    before_action :signed_in_user, only: [:index, :edit, :update, :destroy] 

    def index 
    @keywords = current_user.keywords.to_a 
    end 

    def new 
    @keyword = Keyword.new 
    end 

    def create 
    @keyword = Keyword.find_by(content: params[:content]) 
    if @keyword.nil? 
     @keyword = Keyword.create(keyword_params) 
    end 

    @keyword.associations.create(:user_id => current_user.id) 
    flash[:success] = "Keyword successfully created!" 
    redirect_to keywords_path 
    end 

    def destroy 
    end 

    private 
    def keyword_params 
     params.require(:keyword).permit(:content) 
    end 
end 

回答

0

我不確定爲什麼這可行,但當我將@keyword = Keyword.find_by(content:params [:content])更改爲@keyword =關鍵字.find_by(keyword_params),它的工作原理。

0

這是因爲在情況下,當關鍵字找到你還是指Keyword.new對象,這是不是一個以dB爲單位。

你可以重寫它像這樣:

def create 
    @keyword = Keyword.find_by(content: params[:content]) 
    if @keyword.nil? 
    @keyword = Keyword.create(keyword_params) 
    end 

    @keyword.associations.create(:user_id => current_user.id) 
    flash[:success] = "Keyword successfully created!" 
    redirect_to keywords_path 
+0

感謝Vitaliy爲您的迅速反應。不幸的是,你提出的修改給出了同樣的錯誤 - 「你不能調用創建,除非父母被保存」 – amey1908

+0

它可能是因爲父母有一些驗證錯誤,這將有助於如果你插入關鍵字類的內容 –

+0

您好Vitaliy,我添加代碼爲關鍵字模型和控制器在我原來的問題。如果我的驗證有任何問題,請告訴我。謝謝你的幫助。 – amey1908

0

我想問題可能是您呼叫的關聯模型(通過表)創建的,而不是用戶之一。 Rails的應該關心的關聯模型不是你,所以只是嘗試

@keyword.users.create(:user_id => current_user.id) 

更換

@keyword.associations.create(:user_id => current_user.id) 

如果沒有這個工程,我建議你把一個斷點在控制器中創建指令之前和你必須看看發生了什麼。查看我的最新評論關於如何使用PRY進行調試https://stackoverflow.com/a/21815636/2793607

+0

謝謝Rafa。我想在關鍵字表中使用唯一的關鍵字,但同一個關鍵字可以被多個用戶添加,並且會存儲在關聯表中。例如,我的用戶表中有用戶1和用戶2,關鍵字1和關鍵字2是我的關鍵字表中的唯一記錄。現在,用戶1和用戶2都可以在他們的列表中具有相同的關鍵字 - 「關鍵字1」,並且這將反映在關聯表中。所以關鍵字的唯一性是正確的。我不希望「關鍵字1」在關鍵字表中存儲兩次。 – amey1908

+0

我已經添加了所有型號的代碼 - 用戶,關鍵字和關聯 – amey1908

+0

您現在得到哪個錯誤?打印確切的文字。 –