簡體   English   中英

Rails-建立連接和嵌套表單

[英]Rails - establish_connection and nested forms

我在創建與嵌套模式問題establish_connection使用其他數據庫。

class Restaurant < ActiveRecord::Base
  establish_connection :'f7-api'

  has_many :sections, dependent: :destroy
  has_many :items, through: :sections
  accepts_nested_attributes_for :sections, allow_destroy: true
end

class Section < ActiveRecord::Base
  establish_connection :'f7-api'

  belongs_to :restaurant
  has_many :items, dependent: :destroy
  has_many :options, through: :items
  accepts_nested_attributes_for :items, allow_destroy: true
end

--

PG::ForeignKeyViolation: ERROR: insert or update on table "sections"
 violates foreign key constraint "fk_rails_14e0e2a999" DETAIL: Key
 (restaurant_id)=(3) is not present in table "restaurants". : INSERT INTO
 "sections" ("title_input_id", "restaurant_id", "created_at", 
"updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"

操作的表單參數是(格式化的):

{
  "utf8"=>"✓", "authenticity_token"=>"FLe24nnI3fITIS4bpMBDjJ0Ne+F0S3Rh9HgjYIqotR3CpbT/gHa0c3iQi‌​0yUtiCQNdNBYi0ANN75fqiZU6japw==", 
  "restaurant"=>{
    "name"=>"asd", "business_name"=>"", "cnpj"=>"", "description"=>"", 
    "url"=>"", "phone"=>"", "mobile"=>"", "website"=>"",
    "sections_attributes"=>{
      "1463797768730"=>{"title"=>"", "_destroy"=>"false"}
    }
  }, 
  "commit"=>"Save Restaurant"
}

restaurant_controller

  # POST /restaurants
  # POST /restaurants.json
  def create
    @restaurant = Restaurant.new(restaurant_params)

    respond_to do |format|
      if @restaurant.save
        format.html { redirect_to @restaurant, notice: 'Restaurant was successfully created.' }
        format.json { render :show, status: :created, location: @restaurant }
      else
        format.html { render :new }
        format.json { render json: @restaurant.errors, status: :unprocessable_entity }
      end
    end
  end

def restaurant_params
      params.require(:restaurant).permit(
        :id,
        :name,
        :business_name,
        :cnpj,
        :description,
        :phone,
        :mobile,
        :website,
        :user_id,
        :street,
        :complement,
        :number,
        :lat,
        :lng,
        :zip_code,
        :neighborhood_id,
        :city_id,
        :state_id,
        :country_id,
        photos: [:id, :image, :main],
        open_hours: [:id, :weekday, :opens_at, :closes_at],
        cuisine_ids: [],
        category_ids: [],
        sections_attributes: [
          :id,
          :title,
          :restaurant_id,
          :_destroy,
          items_attributes: [
            :id,
            :title,
            {:description => []},
            :section_id,
            :price_cents,
            :not_equal,
            :_destroy,
            options_attributes: [
              :id,
              {:description => []},
              :item_id,
              :price_cents,
              :_destroy
            ]
          ]
        ]
      )
    end

解決方案是刪除postgres數據庫中的foreign_key引用

我不知道為什么estabilh_connection打破了這種關系。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM