是否有可能有上表中的以下兩列,並將它們保存與active_record:Rails的:ActiveRecord的:: AssociationTypeMismatch錯誤
user_name_id
(這是一個協會)user_name
(只是文本)
爲了證明試圖挽救聯想場user_name_id
和文本字段user_name
下面是型號:
#app/models/blog.rb
class Blog < ApplicationRecord
belongs_to :user_name
end
#app/models/user_name.rb
class UserName < ApplicationRecord
has_many :blogs
end
形式:
<%= form_for(blog) do |f| %>
<% if blog.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(blog.errors.count, "error") %> prohibited this blog from being saved:</h2>
<ul>
<% blog.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :user_name %>
<%= f.text_field :user_name %>
</div>
<div class="field">
<%= f.label :user_name_id %>
<%= f.collection_select :user_name_id, UserName.all, :id, :name %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
強PARAMS:
def blog_params
params.require(:blog).permit(:user_name, :user_name_id)
end
當我爲了創建一個新的blog
提交表單,它出錯了。 params哈希表看起來不錯:
「blog」=>{「user_name_id"=>"1", 「user_name"=>」foo」}
但是我收到以下錯誤:
ActiveRecord::AssociationTypeMismatch UserName(#70119900697480) expected, got String(#70119800086640)
更新:我的理解是,最好是:這些屬性之一的列名應該改變。儘管如此:是否有可能與軌道做到這一點?需要什麼?
顯示博客的兩個模型和控制器 – gates
博客是否屬於名爲user_name或user的表? – gwalshington
屬於一個名爲user_names的實際表格 – Neil