Ruby on rails RubyonRails:在show视图中列出自连接关系的子项

Ruby on rails RubyonRails:在show视图中列出自连接关系的子项,ruby-on-rails,ruby-on-rails-4,rails-activerecord,Ruby On Rails,Ruby On Rails 4,Rails Activerecord,我正在我的应用程序中构建一个层次结构组织列表,使用与我在各种对象中使用的一对多联接相同的原则 但是使用这个一对多自联接,我得到了一个错误,因为生成的SQL引用了一个不存在的Organizations.Organization\u id列 以下是我的部分代码: class Organisation < ActiveRecord::Base validates :owner_id, presence: true validates :status_id, presence: true val

我正在我的应用程序中构建一个层次结构组织列表,使用与我在各种对象中使用的一对多联接相同的原则

但是使用这个一对多自联接,我得到了一个错误,因为生成的SQL引用了一个不存在的Organizations.Organization\u id列

以下是我的部分代码:

class Organisation < ActiveRecord::Base

validates :owner_id, presence: true
validates :status_id, presence: true
validates :playground_id, presence: true
belongs_to :owner, :class_name => "User", :foreign_key => "owner_id"            # helps retrieving the owner name
belongs_to :status, :class_name => "Parameter", :foreign_key => "status_id"     # helps retrieving the status name
belongs_to :parent_org, :class_name => "Organisation", :foreign_key => "parent_id"  # helps retrieving the parent name
has_many :child_orgs, :class_name => "Organisation"                         # links from the child organisations
控制员:

# GET /organisations/1
# GET /organisations/1.json
def show
  @organisation = Organisation.includes(:owner, :status, :parent_org).find(params[:id])
end
列出当前组织子公司的show view摘录:

<table width=100%>    
  <tr><td><hr /></td></tr>
  <tr align="left">
    <th>Linked organisations</th>
    <th></th>
  </tr>
  <tr>
    <td>
      <table class="table table-striped table-condensed">
        <tr align="left">
          <th> Code </th>
          <th> Name </th>
          <th> Description </th>
          <th> Updated by </th>
          <th> Updated at </th>
        </tr>
        <%@organisation.child_orgs.each do |child_org| %>
        <tr align="left">
          <td valign="top"> <%=link_to child_org.code, child_org%> </td>
          <td valign="top"> <%=child_org.name%> </td>
          <td class="col_wide"> <%=child_org.description%> </td>
          <td valign="top"> <%=child_org.updated_by%> </td>
          <td valign="top"> <%=child_org.updated_at%> </td>
        </tr>
        <% end%>
      </table>
    </td>
  </tr>
  <tr>
    <td>
      <%= link_to 'Add organisation', new_organisation_child_org_path(@organisation) %>
    </td>
  </tr>
</table>
我的问题:

1-如何避免此错误

2-新的_Organization_child_Organization_path(@Organization)路径对于创建子组织是否正确

非常感谢你的帮助

致以最良好的祝愿


Fred

您需要在上指定外键有多个关联:

has_many :child_orgs, :class_name => "Organisation" , :foreign_key => 'parent_id'

在您的情况下,最好使用finder\u sql或scope,这取决于您选择子组织时使用的rails版本,以避免将child\u id属性添加到您的organizations表中

in organization.rb

轨道3

has_many :child_orgs, class_name: "Organization", finder_sql: proc { "SELECT * FROM organizations WHERE parent_id = #{id}" }
轨道4(未测试)

你需要打电话的路线

new_organization_organizations_path(@organization)

键入console
rake routes
查看所有可用的路由

sry使用buldozer for smth simple:)您刚刚错过了has_manyI中的外键属性。我认为对于Rails 4,->{}位必须在类名称:位之前。但即使我这么说,我也会得到一个关于id未定义的错误。。。
has_many :child_orgs, class_name: "Organization", finder_sql: proc { "SELECT * FROM organizations WHERE parent_id = #{id}" }
has_many :child_orgs, ->(id) { where(parent_id: id) }, class_name: "Organization"
new_organization_organizations_path(@organization)