2015-04-05 71 views
1

我正在嘗試使用Ruby on Rails 4.2.1和PostgreSQL創建應用程序。無法將多租戶屬性添加到嵌套模型

我有以下型號。

class Builder < ActiveModel 
    has_many :bills 
    has_many :bills_partials 
end 

class Bill < ActiveModel 
    belongs_to :builder 
    has_many :bills_partials 

    accepts_nested_attributes_for :bills_partial, :reject_if => :all_blank, :allow_destroy => true 
end 

class BillPartial < ActiveModel 
    belongs_to :builder 
    belongs_to :bills 
end 

,並執行以下操作

def new 
    bill.bills_partials.build 
end 

def create 
    @bill = Bill.scoped_to(current_builder).new(bill_partial_params) 
    if @bill.save 
    redirect_to bills_path, flash: { success: t('flash.generic.female.created', model: Bill.model_name.human) } 
    else 
    render :new 
    end 
end 

private 
    def bill 
    @bill ||= Bill.scoped_to(current_builder).find_by(id: uuid_or_nil(params[:id])) || Bill.scoped_to(current_builder).new 
    end 
    helper_method :bill 

    def bill_params 
    params 
     .require(:bill) 
     .permit(:description, 
       :value, 
       :date, 
       bills_partials_attributes: [:id, 
              :due_date, 
              :valor, 
              :_destroy]) 

    end 

的PARAMS的輸出是:

{ 
    "description"=>"some desc", 
    "value"=>"123", 
    "date"=>"10/10/2010", 
    "bills_partials_attributes" => 
    { 
    "0" => 
    { 
     "due_date"=>"14/03/2003", 
     "value"=>"123" 
    }, 
    "1" => 
    { 
     "due_date"=>"14/03/2003", 
     "value"=>"123" 
    } 
    } 
} 

的事情是,我要補充current_builder.id到每一個模型,BillBillPartial例如。將current_builder.id添加到BillPartial模型上的builder_id的最佳方法是什麼?

回答

0

嘛,找東西后,我通過定義

before_create do 
    self.builder_id = bill.try(:builder_id) 
end 

在BillPartial模型解決了這個。