我不知道oracle_enhanced适配器是否有问题。我有:

class Room < ActiveRecord::Base
  has_and_belongs_to_many :manuals
end

class Manual < ActiveRecord::Base
  has_and_belongs_to_many :rooms
end

class CreateJoinTableManualRoom < ActiveRecord::Migration
  def change
    create_join_table :manuals, :rooms do |t|
      t.index [:manual_id, :room_id]
      t.index [:room_id, :manual_id]
    end
  end
end


创建新手册时,它不会更新我的manuals_rooms连接表。

class ManualsController < ApplicationController
  before_action :set_manual, only: [:show, :edit, :update, :destroy]

  def index
    @manuals = Manual.all
    @rooms = Room.all
  end

  def show
  end

  def new
    @manual = Manual.new
  end

  def edit
  end

  def create
    @manual = Manual.new(manual_params)

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

  def update
    respond_to do |format|
      if @manual.update(manual_params)
        format.html { redirect_to @manual, notice: 'Manual was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @manual.errors, status: :unprocessable_entity }
      end
    end
  end

  private
    def set_manual
      @manual = Manual.find(params[:id])
    end

    def manual_params
      params.require(:manual).permit(:name, :document, :room_ids => [])
    end
end


日志

Started POST "/manuals" for 127.0.0.1 at 2014-08-20 09:12:12 -0400
Processing by ManualsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Ntqqh1vU9zrGZuw3K8xNef440ktAixWj+6Cx20wrCRg=", "manual"=>{"document"=>#<ActionDispatch::Http::UploadedFile:0x000001134643d0 @tempfile=#<Tempfile:/var/folders/d1/x0nbfyrj30bd_p33ds0f12_c0000gq/T/RackMultipart20140820-59361-1jkeynu>, @original_filename="103.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"manual[document]\"; filename=\"103.jpg\"\r\nContent-Type: image/jpeg\r\n">, "room_id"=>"3"}, "commit"=>"Upload"}
Unpermitted parameters: room_id
  SQL (1.8ms)  INSERT INTO "MANUALS" ("CREATED_AT", "DOCUMENT", "ID", "UPDATED_AT") VALUES (:a1, :a2, :a3, :a4)  [["created_at", Wed, 20 Aug 2014 09:12:12 EDT -04:00], ["document", "103.jpg"], ["id", 10021], ["updated_at", Wed, 20 Aug 2014 09:12:12 EDT -04:00]]
Redirected to http://localhost:3000/manuals/10021
Completed 302 Found in 16ms (ActiveRecord: 1.8ms)


我将room_ids => []更改为room_id,现在我得到:

  SQL (2.5ms)  INSERT INTO "MANUALS" ("CREATED_AT", "DOCUMENT", "ID", "ROOM_ID", "UPDATED_AT") VALUES (:a1, :a2, :a3, :a4, :a5)  [["created_at", Wed, 20 Aug 2014 09:20:43 EDT -04:00], ["document", "AMH_BW.jpg"], ["id", 10022], ["room_id", 3], ["updated_at", Wed, 20 Aug 2014 09:20:43 EDT -04:00]]
Redirected to http://localhost:3000/manuals/10022
Completed 302 Found in 56ms (ActiveRecord: 19.0ms)


manuals / _form.html.erb

<%= form_for(@manual) do |f| %>
  <div class="field">
    <%= f.label :document %><br>
    <%= f.file_field :document %>
  </div>
  <div class="field">
    <%#= collection_select(:manual, :room_ids, Room.all, :id, :name) %>
    <%= f.grouped_collection_select :room_id, Building.order(:name), :rooms, :name, :id, :name_with_number, include_blank: true %>
  </div>
  <div class="actions">
    <%= f.submit "Upload" %>
  </div>
<% end %>

最佳答案

我在这里看到几个问题。让我一一强调它们:

1.缺少multiple: true选项

RoomManual具有HABTM关联,即M-M关系。

正如@MaxWilliams在他的答案中指出的那样,Rails期望room_ids哈希中的params数组而不是单个值。

为此,您应该允许用户通过在rooms中的multiple: true方法中添加html作为grouped_collection_select选项的形式(记住其M-M关系)来选择多个form。而且,第一个参数应为room_ids而不是room_id

<%= f.grouped_collection_select :room_ids, Building.order(:name), :rooms, :name, :id, :name_with_number, {include_blank: true}, {multiple: true} %>


2.允许room_ids作为数组

由于上面的更改#1,由于room_ids现在将作为数组传递到params哈希中,因此应允许将其作为Array

def manual_params
  params.require(:manual).permit(:name, :document, :room_ids => [])
end


3.从room_id表中删除manuals

根据问题中的共享日志

SQL (2.5ms)  INSERT INTO "MANUALS" ("CREATED_AT", "DOCUMENT", "ID", "ROOM_ID", "UPDATED_AT") VALUES (:a1, :a2, :a3, :a4, :a5)  [["created_at", Wed, 20 Aug 2014 09:20:43 EDT -04:00], ["document", "AMH_BW.jpg"], ["id", 10022], ["room_id", 3], ["updated_at", Wed, 20 Aug 2014 09:20:43 EDT -04:00]]


我看到您已经在room_id表中添加了manuals字段。现在,这不是!没有!

您在ManualRoom之间没有1-M关联或1-1关联,但是它们之间的M-M关联与联接表manuals_rooms已经包含room_idmanual_id字段。

底线是您需要从room_id表中删除manuals

生成迁移以删除room_id为:

rails generate migration RemoveRoomIdFromManuals room_id:integer


运行rake db:migrate迁移更改。

这应该解决您的问题。让我知道。




我真的开始认为这可能是一个预言


没有! oracle_enhanced adapter没有任何问题。导致此问题的是您的代码。

关于ruby-on-rails - 在oracle_enhanced适配器上具有has_and_belongs_to_many,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25405516/

10-08 23:13