Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails-关联字段未按外部表的预期工作。_Ruby On Rails - Fatal编程技术网

Ruby on rails Rails-关联字段未按外部表的预期工作。

Ruby on rails Rails-关联字段未按外部表的预期工作。,ruby-on-rails,Ruby On Rails,我正在使用rails连接到外部数据库。我有一个有字段的opportunities表 如果我使用以下代码,我会得到公司名称 <% company_id = opportunity.Oppo_PrimaryCompanyId %> <% @company = CrmTable::Company.where("Comp_CompanyId = ?", company_id) %> <% @company.each do |company|%> <td&

我正在使用rails连接到外部数据库。我有一个有字段的opportunities表

如果我使用以下代码,我会得到公司名称

<% company_id = opportunity.Oppo_PrimaryCompanyId %>
<% @company = CrmTable::Company.where("Comp_CompanyId = ?", company_id) %>
<% @company.each do |company|%>
    <td>
      <%= company.Comp_Name %>

    </td>
<% end %>

然而,基于我对Rails数据库的关联所做的工作,我认为我可以做到

<% company_id = opportunity.Oppo_PrimaryCompanyId %>
<% @company = CrmTable::Company.where("Comp_CompanyId = ?", company_id) %>
<%= @company.Comp_Name %>

当我尝试时,我得到一个错误

   NoMethodError in Searches#show
Showing C:/Users/cmendla/RubymineProjects/product_development/app/views/shared/_opportunity_list.html.erb where line #56 raised:

undefined method `Comp_Name' for #<Company::ActiveRecord_Relation:0xa734738>
Trace of template inclusion: app/views/searches/show.html.erb

Rails.root: C:/Users/cmendla/RubymineProjects/product_development

Application Trace | Framework Trace | Full Trace
app/views/shared/_opportunity_list.html.erb:56:in `block in _app_views_shared__opportunity_list_html_erb__1021919415_87714420'
app/views/shared/_opportunity_list.html.erb:29:in `each'
app/views/shared/_opportunity_list.html.erb:29:in `_app_views_shared__opportunity_list_html_erb__1021919415_87714420'
app/views/searches/show.html.erb:16:in `_app_views_searches_show_html_erb__1026643635_104558460'
Request

Parameters:

{"id"=>"180"}
NoMethodError在搜索中#显示
显示C:/Users/cmendla/RubymineProjects/product_development/app/views/shared/u opportunity_list.html.erb,其中第56行出现:
未定义的方法“Comp_Name”#
模板包含跟踪:app/views/searches/show.html.erb
root:C:/Users/cmendla/RubymineProjects/product\u development
应用程序跟踪|框架跟踪|完整跟踪
app/views/shared/_opportunity_list.html.erb:56:in`block in_app_view_shared_opportunity_list_html_erb_1021919415_87714420'
app/views/shared/_opportunity_list.html.erb:29:in'each'
app/views/shared/_opportunity_list.html.erb:29:in`_app_view_shared_opportunity_list_html_erb_1021919415_87714420'
app/views/searches/show.html.erb:16:in`_app_views_searches_show_html_erb_1026643635_104558460'
要求
参数:
{“id”=>“180”}
如果我执行@company.inspect,我会看到与当前opportunity对应的id如下

 #<ActiveRecord::Relation [#<Company Comp_CompanyId: 7, Comp_PrimaryPersonId: 7, Comp_PrimaryAddressId: 13, Comp_PrimaryUserId: 35, Comp_Name: "XXX CORPORATION", Comp_Type: "Customer", Comp_Status: "Active", . . .
#
但很明显,您希望将变量重命名为companys并相应地向前移动

<% @company.each do |company|%>
    <td>
      <%= company.Comp_Name %>

    </td>
<% end %>
但很明显,您希望将变量重命名为companys并相应地向前移动

<% @company.each do |company|%>
    <td>
      <%= company.Comp_Name %>

    </td>
<% end %>



@company.map(&:Comp_name)
<% @company.each do |company|%>
    <td>
      <%= company.Comp_Name %>

    </td>
<% end %>
<% @company = CrmTable::Company.where("Comp_CompanyId = ?", company_id).first %>
<% @company = CrmTable::Company.find_by_Comp_CompanyId(company_id) %>
 <% if @company %>
   <%= @company.Comp_Name %>
  <% end %>