2017-03-31 97 views
0

是否有可能有上表中的以下兩列,並將它們保存與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)

更新:我的理解是,最好是:這些屬性之一的列名應該改變。儘管如此:是否有可能與軌道做到這一點?需要什麼?

+0

顯示博客的兩個模型和控制器 – gates

+0

博客是否屬於名爲user_name或user的表? – gwalshington

+0

屬於一個名爲user_names的實際表格 – Neil

回答

1

的問題是,從上面的答案顯而易見 - 改變user_name屬性被稱爲別的東西。

作爲最後的手段,這可能是一個壞的建議,但你甚至可以嘗試申報哪個屬性您要使用如:

class Blog < ApplicationRecord 
    belongs_to :user_name, through: :user_name_id 
end 

但同樣,原因你關聯的記錄,這樣你就可以在任何時候通過關聯調用user_name,並獲得存儲在那裏的所有信息。這意味着,您不需要將user_name與博客一起存儲......因爲您已經通過了user_name關聯。

+0

沒有必要通過;) – gates

+0

如果你保留了這兩個屬性,通過聲明你正在關聯的屬性來分開它們。這就是爲什麼他首先解決了這個問題。 – gwalshington

+0

不幸的是,在應用程序中,無法更改兩個表列名稱。帶'_id'的列表示關聯(如果存在),沒有'_id'的列表示集合中未列出的名稱。 – Neil

0

當你這樣做:

belongs_to :user_name 

ActiveRecord的假設Blog有一個屬性user_name_id(它看起來你有 - 到目前爲止,那麼好)。如Guide中所述,您還會收到一個setter方法,user_name=,期望您將其傳遞給UserName實例。

當你做這樣的事情:

Blog.new(「user_name_id"=>"1", 「user_name"=>」foo」) 

ActiveRecord的期待user_nameUserName類的一個實例。但你只是通過了String。這正是錯誤告訴你的。

另外,我同意阿列克謝。我不知道爲什麼你會堅持user_name(作爲字符串)在Blog。如果該類UserNamename屬性,那麼你永遠可以這樣做:

blog.user_name.name 

我也同意這阿列克謝是UserName一個奇怪的類名。阿列克謝關於命名的建議是很好的,所以請考慮他們。

+0

請參閱最新的問題。這只是一個快速演示代碼來演示這個問題。 – Neil

1
  1. 如果您已經使用belongs_to'user_name',則根本不應該使用'user_name'字段。
  2. 請不要調用Model'UserName'這不好,也許這是UserProfile或只是用戶?
  3. 您確定要爲您的博客緩存user_name嗎?如果用戶改變他的名字,將會產生很多問題。如果您想緩存user_name for blog,您可以重命名'user_name_cached'列,並在'UserName'上更新它將更新before_save回調。
  4. 對作者的更新

UPDATE:

belongs_to :your_new_belons_to_name, class_name: 'NameUser', foreign_key: 'name_user_id'

+0

請查看最新的問題。這只是一個快速演示代碼來演示這個問題。 – Neil

+0

好吧請看我的更新 – AlexeyKuznetsov

+0

如果你想安全原始的'user_name'方法名我認爲你必須編寫你自己的getter/setter方法 'def user name; UserName.where(id:self.user_name_id).first || self.user_name; end ' – AlexeyKuznetsov

相關問題