oracle_enhanced适配器上的has_and_belongs_to_many

时间:2014-08-20 13:05:51

标签: ruby-on-rails activerecord ruby-on-rails-4

我不知道这是否是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)

手册/ _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 %>

2 个答案:

答案 0 :(得分:5)

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

1。缺少multiple: true选项

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

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

要实现这一点,您应该允许用户在表单中选择多个rooms (请记住,其 MM关系),方法是添加{{1} } multiple: true作为html grouped_collection_select方法的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作为数组

由于上面的 Change#1 room_ids现在将作为params哈希中的数组传递,因此应该允许Arraydef manual_params params.require(:manual).permit(:name, :document, :room_ids => []) end

room_id

3。从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字段添加到Manual表。现在,这是一个不!不!

您在Roommanuals_rooms之间没有 1-M关联 1-1关联但是它们之间的 MM关联具有已包含room_idmanual_id字段的联接表room_id

从根本上说,您需要从manuals表中删除 room_id

生成迁移以删除rails generate migration RemoveRoomIdFromManuals room_id:integer

rake db:migrate

运行oracle_enhanced adapter以迁移更改。

这应该照顾你的问题。让我知道。


  

我真的开始认为这可能是一件神奇的事情

没有! {{1}}没有任何问题。它是导致问题的代码。

答案 1 :(得分:1)

由于它是一种关系,你可能会得到params[:room_ids],并且这个数字就是一个数组。表单中输入的实际名称应为room_ids[] - 末尾的数组告诉rails收集名为room_ids[]的所有参数,并将它们收集到params[:room_ids]中的数组中。 / p>

因此,在您渲染的HTML中,您希望看到(例如,如果它是一个复选框),就像这样:

<input name="room_ids[]" type="checkbox" value="3" />

如果您的渲染html中没有(请查看带有Chrome检查器的页面或类似内容),那么您并未正确设置表单。请在另一个编辑中添加表单代码到您的帖子。