2016-05-21 23 views
0

Ich habe ein Problem beim Erstellen eines verschachtelten Modells mit establish_connection mit einer anderen Datenbank.Rails - Herstellen einer Verbindung und verschachtelte Formulare

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" 

Die Formparameter aus der Aktions werden (formatiert):

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

Können Sie die ActiveRecord-Anweisung anzeigen, die Sie dafür verwenden? –

+0

{ "UTF-8" => "✓", "authenticity_token" => "FLe24nnI3fITIS4bpMBDjJ0Ne + F0S3Rh9HgjYIqotR3CpbT/gHa0c3agfgRt3QNdNBYi0ANN75fqiZU6japw ==", "Restaurant" => { "name" => "asd", "business_name" =>“ " "CNPJ"=> "", "Beschreibung"=> "", "url"=> "", "phone"=> "", "mobile"=> "", " Website "=>" ", " sections_attributes "=> {" 1463797768730 "=> {" title "=>" ", " _destroy "=>" false "}}}, " commit "=>" Restaurant speichern " } –

+0

Gibt es einen Eintrag in Ihrer Restauranttabelle mit der ID '3'? Was ist das Ergebnis von 'Restaurant.find (3)' in rails console? –

Antwort

0

Die Lösung FOREIGN_KEY Referenzen in postgres zu entfernen, ist Datenbank

Ich weiß nicht, warum estatiilh_connection diese Beziehung bricht.

Verwandte Themen