2016-05-21 2 views
0

У меня возникла проблема с созданием вложенной модели с помощью establish_connection с использованием другой базы данных.Rails - установить_соединение и вложенные формы

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 
+2

Можете ли вы показать оператор ActiveRecord, который вы используете для этого? –

+0

{ "utf8" => "✓", "authenticity_token" => "FLe24nnI3fITIS4bpMBDjJ0Ne + F0S3Rh9HgjYIqotR3CpbT/gHa0c3agfgRt3QNdNBYi0ANN75fqiZU6japw ==", "ресторан" => { "имя" => "ASD", "BUSINESS_NAME" =>» " "CNPJ"=> "", "описание"=> "", "URL"=> "", "телефон"=> "", "мобильный"=> ""," сайт "=>" ", " sections_attributes "=> {" 1463797768730 "=> {" title "=>" ", " _destroy "=>" false "}}}, " commit "=>" Save Restaurant " } –

+0

Есть ли запись в таблице ваших ресторанов с идентификатором '3'? В консоли rails, что является результатом 'Restaurant.find (3)'? –

ответ

0

Решение состоит в том, чтобы удалить foreign_key ссылки в Postgres база данных

Я не знаю, почему Estabilh_connection нарушает эти отношения.