【问题标题】:Rails 3.2.x Undefined Method 'join'Rails 3.2.x 未定义方法“加入”
【发布时间】:2012-04-26 02:25:33
【问题描述】:

我有一个具有多对多关系的篮球应用程序,其中一名教练可以执教多个球队,而一个球队可以有多个教练。

Coaches_Controller.rb

  def index
    @coaches = Coach.joins(:teams).select("coaches.first_name, coaches.last_name, teams.team_level")

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @coaches }
    end
  end

Index.html.erb

<% @coaches.each do |coach| %>
      <tr>
        <td><%= link_to coach.first_name, coach_path(coach) %></td>
        <td><%= coach.last_name %></td>
        <td><%= coach.team_level %></td>
        <td>
          <%= link_to t('.edit', :default => t("helpers.links.edit")),
                      edit_coach_path(coach), :class => 'btn btn-mini' %>
          <%= link_to t('.destroy', :default => t("helpers.links.destroy")),
                      coach_path(coach),
                      :method => :delete,
                      :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')),
                      :class => 'btn btn-mini btn-danger' %>
        </td>
      </tr>
    <% end %>

我收到了这个错误,我不太清楚为什么......

http://i.stack.imgur.com/5a6oB.png

想法?我觉得这是我看不到的小东西……谢谢!

【问题讨论】:

    标签: sql ruby-on-rails activerecord join ruby-on-rails-3.2


    【解决方案1】:

    我可以看到的一件事是错误的,您的select 中没有coaches.id。您需要id 才能使coach_path(coach) 工作。试试这个:

    @coaches = Coach.joins(:teams).select("coaches.id, coaches.first_name, coaches.last_name, teams.team_level")
    

    不确定这是否能解决您遇到的 join 错误。

    【讨论】:

    • 啊,小提琴棒,我知道这很容易,我看过了。谢谢你让我觉得自己很笨;P
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-01
    • 1970-01-01
    • 2012-03-15
    • 1970-01-01
    • 2016-08-16
    • 2013-04-09
    • 2013-02-04
    相关资源
    最近更新 更多