简体   繁体   English

Rails-建立连接和嵌套表单

[英]Rails - establish_connection and nested forms

I'm having a problem creating a nested model with establish_connection using another database. 我在创建与嵌套模式问题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"

The form parameters from the action are (formatted): 操作的表单参数是(格式化的):

{
  "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"
}

restaurants_controller 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

The solution is to remove foreign_key references in postgres database 解决方案是删除postgres数据库中的foreign_key引用

I dont know why estabilh_connection is breaking this relationship. 我不知道为什么estabilh_connection打破了这种关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM