Rails - establish_connection和嵌套表单

时间:2016-05-21 02:35:32

标签: ruby-on-rails database nested foreign-keys

我在使用其他数据库创建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"
}

restaurants_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

1 个答案:

答案 0 :(得分:0)

解决方案是删除postgres数据库中的foreign_key引用

我不知道为什么establishilh_connection打破了这种关系。