gpt4 book ai didi

ruby-on-rails - oracle_enhanced 适配器上的 has_and_belongs_to_many

转载 作者:行者123 更新时间:2023-12-04 03:35:26 24 4
gpt4 key购买 nike

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

最佳答案

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

1。缺少 multiple: true 选项

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

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

要做到这一点,您应该允许用户在表单中选择多个房间(记住,它的M-M 关系)通过添加 multiple: true 作为 formgrouped_collection_select 方法的 html 选项。而且,第一个参数应该是 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 作为数组

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

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

3。从 manuals 表中删除 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]]

我看到您已将 room_id 字段添加到 manuals 表中。现在这是一个不!不!

1-M 关联1-1 关联 ManualRoom 但它们之间有一个M-M 关联,连接表 manuals_rooms 已经包含 room_idmanual_id 字段。

最重要的是,您需要从 manuals 表中删除 room_id

生成用于删除 room_id 的迁移:

rails generate migration RemoveRoomIdFromManuals room_id:integer

运行 rake db:migrate 以迁移更改。

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


I'm really starting to think that this may be an oracle thing

不! oracle_enhanced 适配器 没有任何问题。是您的代码导致了问题。

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com