【问题标题】:ActiveRecord::AssociationTypeMismatch. has_many relationshipActiveRecord::AssociationType 不匹配。 has_many 关系
【发布时间】:2012-12-03 01:31:07
【问题描述】:

我是以下错误

ActiveRecord::AssociationTypeMismatch in ProposalsController#create

User(#78682120) expected, got String(#68929150)
Rails.root: /home/james/app

Application Trace | Framework Trace | Full Trace
app/controllers/proposals_controller.rb:76:in `new'
app/controllers/proposals_controller.rb:76:in `create'
Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"0qTzQ/KMA2Ch60aajSg265tThfCTBCB7w0rS8nD4Qwg=",
 "proposal"=>{"phones"=>"",
 "broadband"=>"",
 "fixed_line"=>"",
 "group_calling"=>"",
 "frequent_txt"=>"",
 "frequent_data"=>"",
 "type"=>"TelecommunicationsProposal",
 "contact_type"=>"",
 "extra_options"=>"",
 "current_provider"=>"",
 "closing_date"=>"03/12/2012",
 "users"=>["#<User:0x961c9e4>"]},
 "commit"=>"Create Proposal"}

我正在尝试建立这样一种关系,即一个用户有很多提案,并且该提案可以有很多用户。

我可以为每个用户获取提案,但也需要反过来。

提案.rb

 class Proposal < ActiveRecord::Base
      has_many :users
    ...

TelcommunicationsProposal.rb

class TelecommunicationsProposal < Proposal
  belongs_to :users
  after_create :proposal_creation
  ...

提案控制器.rb 第 76 行出现错误,即 Proposal.new 创建

 def create
    @proposal = Proposal.new(params[:proposal])

    if current_user
     @user = current_user

app/views/telecommunications_proposal/_form.html.erb 的小节选

 <% for user in User.find(:all) %>  
    <div>  
      <%= check_box_tag "proposal[users][]",user%>  
      <%= user.trading_name %>  
    </div>  
<% end %> 

谁能告诉我哪里出错了?

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 activerecord has-many


    【解决方案1】:

    看起来您应该将用户的id 传递给check_box_tag,而不是记录本身,并在复选框名称中使用user_ids 而不是users

    <%= check_box_tag "proposal[user_ids][]", user.id %>
    

    参考:

    【讨论】:

      猜你喜欢
      • 2012-12-24
      • 1970-01-01
      • 2015-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-19
      相关资源
      最近更新 更多