Rails 3.2.x未定义的方法'join'

时间:2012-04-26 02:25:34

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

我有一个多对多关系的篮球应用程序,Coach可以指导多个团队,一个团队可以有很多教练。

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

想法?我觉得这是小事,我没有看到......谢谢!

1 个答案:

答案 0 :(得分:1)

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

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

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