2012-02-11 86 views
0

我被困在嵌套形式和活动记录使用中间的“find_or_create_by”方法..我试图做的事:嵌套形式:find_or_create_by多个字段

我有3种型号:帐户,交易和类别。

class Account < ActiveRecord::Base 
    has_many :transactions, :dependent => :destroy 
    has_many :categories, :dependent => :destroy 
end 

class Category < ActiveRecord::Base 
    has_many :transactions 
    belongs_to :account 
end 

class Transaction < ActiveRecord::Base  
    belongs_to :category, :autosave => true 
    belongs_to :account 

    accepts_nested_attributes_for :category 
end 

我的形式是这样的:应用程序/视图/交易/ new.haml

= semantic_form_for @transaction do |f| 
    = f.inputs do 
    = f.input :account_id, :as => :hidden 
    = f.input :title, :label => false 
    = f.input :amount, :label => false 
    = f.inputs :for => :category do |c| 
     = c.input :title, :as => :string 
     = c.input :account_id, :as => :hidden 
    = f.buttons do 
    = f.submit "Save" 

我的控制器是这样的:

class TransactionsController < ApplicationController 

    def new 
    @transaction = Transaction.new 
    @transaction.date ||= Transaction.last.date if Transaction.last 
    @transaction.account= Account.find(params[:account]) if params[:account] 
    @account = @last_transaction.account if @last_transaction 
    @account = Account.find(params[:account]) if params[:account] 
    @transaction.build_category(:account => @account) 
    end 

    def create 
    @transaction = Transaction.new(params[:transaction]) 
    @account = @transaction.account 
    respond_to do |format| 
     if @transaction.save 
     format.html {redirect_to (new_transaction_path(:account => @account))} 
     else 
     format.html {redirect_to (new_transaction_path(:account => @account))} 
     end 
    end 
    end 
end 

类别控制器:

class CategoriesController < ApplicationController 

    before_filter :authenticate_user! 

    def new 
    @category = Category.new 
    @category.account = Account.find(params[:account]) if params[:account] 
    @accounts = current_user.accounts 
    end 

    def create 
    @category = Category.new(params[:category]) 
    respond_to do |format| 
     if @category.save 
     format.html {redirect_to (categories_path)} 
     else 
     format.html {render :action => "new"} 
     end 
    end 
    end 

    def update 
    @category = Category.find(params[:id]) 
    respond_to do |format| 
     if @category.update_attributes(params[:category]) 
     format.html {redirect_to (categories_path)} 
     else 
     format.html {render :action => "edit"} 
     end 
    end 
    end 
end 

现在,我卡住了。我只想创建一个新类别,如果还没有一个具有相同标题和相同account_id的现有类别。

现在,它总是创建一个新的类别,忽略已经有一个具有相同名称和相同account_id的类别。我知道,我应该使用这样的事情:

Category.find_or_create_by_title_and_account_id(category.title, account.id) 

但我应该在哪里使用它,它应该如何看到底是什么?

我真的很感谢你的帮忙!

+0

那么你的控制器代码在哪里? – fl00r 2012-02-11 10:53:10

+0

我添加了控制器代码。 – algi 2012-02-11 11:01:37

回答

0

您可以使用Category.find_or_initialize_by_title_and_account_id(category.title, account.id),然后修改它并保存(_create_保存在同一行中的对象)。但如果我理解正确,你需要的是validations。按照惯例,我们设计我们的应用程序来检索控制器的#create动作中的错误(如:“类别标题必须是唯一的”),如果有错误,我们再次render :action => :new(具有与先前相同的形式,但显示错误)。我发现你使用的是formtastic,所以你默认情况下会在窗体中显示错误(我认为)。

+0

感谢您的回答。但是,我会在哪里放置你的建议线?哪个控制器和哪个动作? – algi 2012-02-11 17:54:16

+0

您在控制器中拥有它,因为这是通常和最合适的方式。我发现你的代码存在一些问题,你有没有阅读rails guide?你的控制器中'@ last_transaction'的定义在哪里? – farnoy 2012-02-11 19:28:56

+0

你可以尝试用'@transaction.category = Category.find_or_initialize_by_account_id_and_transaction_id(@ account.id,@ transaction.id)替换'@ transaction.build_category(:account => @account)'' – farnoy 2012-02-11 19:34:28