ruby-on-rails - ActiveRecord::关联类型不匹配。有很多关系

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

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

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

提案.rb

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

电信提案.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/te communications_proposal/_form.html.erb 的小摘录

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

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

最佳答案

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

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

引用资料:

关于ruby-on-rails - ActiveRecord::关联类型不匹配。有很多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13675956/

相关文章:

ruby - 如何让 Selenium/Ruby 机器人在执行操作之前等待?

ruby-on-rails - Rails - 如何创建链接到另一个模型的两个模型

ruby-on-rails - Ruby 生产服务器内存泄漏

sql - 如何在 SQLite3 和 Rails 3.1 中打开 REGEXP?

ruby-on-rails-3 - 设计辅助方法,确认!无法在出厂设置中使用/找到

ruby-on-rails - Rails应用程序流程图生成?

jquery - 如何使用 Rails 3 更新到 jQuery 1.7+?

ruby-on-rails - 当应该采取行动挽救异常时,帮助让 Rails Controller 测试通过

ruby-on-rails - 根据其子项(或没有子项)的属性的 Rails 范围

ruby - 为什么 String#split ("\n") 和 Array#join (' ' ) 比 String#gsub(/\n/, ' ' ) 更快?