2012-12-03 107 views
2

我下面的错误ActiveRecord :: AssociationTypeMismatch。的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 
    ... 

上线76 TelcommunicationsProposal.rb

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

ProposalsController.rb 错误符这是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 %> 

有人可以告诉我我要去哪里吗?

回答

3

看起来你应该传递用户的id到,而不是记录本身,而在复选框的名称,而不是users使用user_ids

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

参考文献: