ActiveRecord的:: AssociationTypeMismatch。有很多关系

时间:2012-12-03 01:31:07

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

我是以下错误

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"}

我正在尝试建立一种关系,以便用户拥有许多提案,并且该提案可以包含许多用户。

我知道我可以为每个用户提供建议,但也需要反过来。

Proposal.rb

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

TelcommunicationsProposal.rb

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

ProposalsController.rb 第76行出现错误,即Proposal.new创建

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

    if current_user
     @user = current_user

摘自app / views / telecommunication_proposal / _form.html.erb

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

有人可以告诉我哪里出错了吗?

1 个答案:

答案 0 :(得分:3)

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

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

参考文献: