Ruby on rails HABTM把我搞砸了-Rails 3

Ruby on rails HABTM把我搞砸了-Rails 3,ruby-on-rails,activerecord,ruby-on-rails-3.1,has-and-belongs-to-many,Ruby On Rails,Activerecord,Ruby On Rails 3.1,Has And Belongs To Many,我在用户和孩子之间建立了一个简单的HABM关系 create_table "children", :force => true do |t| t.string "first_name" t.string "last_name" t.boolean "enrolled", :default => true t.datetime "unenrolled_datetime" end 如果一个子对象属于您,我希望您查看

我在用户和孩子之间建立了一个简单的HABM关系

  create_table "children", :force => true do |t|
    t.string   "first_name"
    t.string   "last_name"
    t.boolean  "enrolled",            :default => true
    t.datetime "unenrolled_datetime"
  end
如果一个子对象属于您,我希望您查看该子对象以及该子对象所属的其他用户的名称。由于某些原因,我无法打印该子项所属的其他用户的名称

我选择如下:

@children = Child.all(:include => :users, :conditions => ["users.id = ? AND enrolled = ?", current_user.id, true])
<% for user in child.users%>
  <% if can? :manage, Child %>
    <a rel='tipsy'><%= link_to '[#{user.first_and_last_name}]', edit_child_path(child), :title => "#{user.updateChoice}"%></a>
  <%else%>  
    <a rel='tipsy'><%= link_to '[#{user.first_and_last_name}]', new_parentnote_path, :title => "#{user.updateChoice}"%></a>
  <%end%>
<%end%>
并尝试打印如下内容:

@children = Child.all(:include => :users, :conditions => ["users.id = ? AND enrolled = ?", current_user.id, true])
<% for user in child.users%>
  <% if can? :manage, Child %>
    <a rel='tipsy'><%= link_to '[#{user.first_and_last_name}]', edit_child_path(child), :title => "#{user.updateChoice}"%></a>
  <%else%>  
    <a rel='tipsy'><%= link_to '[#{user.first_and_last_name}]', new_parentnote_path, :title => "#{user.updateChoice}"%></a>
  <%end%>
<%end%>
然后我有了孩子

  create_table "children", :force => true do |t|
    t.string   "first_name"
    t.string   "last_name"
    t.boolean  "enrolled",            :default => true
    t.datetime "unenrolled_datetime"
  end
和用户

  create_table "users", :force => true do |t|
    t.string   "email"
    t.string   "encrypted_password"
    t.string   "first_name"
    t.string   "last_name"
    ........   other stuff to reset password etc
  end

我相信这是条件users.id=,current_user.id是当前用户,它只加载当前_用户的子级,随后当您迭代这些子级时,它是那里唯一的用户

您可能希望根据子项进行选择。用户\u id:


您试图获取一个用户的子用户,同时获取所有其他父用户

列出用户的子项并显示每个子项的父项的简单方法如下:

@children = current_user.children

<%= @children.each do |child| %>
    <h2><%= child.first_name %></h2>
    <%= @child.parents.each do |parent| %>
       <p><%= parent.first_name %></p>
    <% end %>
<% end %>

谢谢你的回复,克莱夫。但是,我没有child.user\u id列。我尝试了您的建议,得到了PGError:ERROR:column children.user\u id不存在。目前在开发中。数据库中只有2个子项。两者都有多个用户。我很困惑为什么它不起作用。请提供有关您的模式和您正在尝试完成的内容的所有详细信息。添加了有关我正在尝试完成的内容的模式和详细信息。谢谢你的帮助,克莱夫。你的链接中的引号看起来是错误的……谢谢你的帮助。这是一个问题,当我拿出div在这里过去,因为div与问题无关。为了清楚起见,把潜水艇放回原处。罗宾,你救了这一天。非常感谢。我不知道用户有多少:通过,但你的例子与我目前的HABTM工作。我用@children=current\u user.children`替换了@children=Child.all:include=>:users,:conditions=>[users.id=?AND registered=?,current\u user.id,true]。我本该想到/试一下的。荣誉我的荣幸;此外,这是一个细节,但如果可以:管理,子部分可能在循环之外:can\u manage\u children=can?:管理,孩子;然后在循环中,只需执行{user.updateChoice}%>。此外,您正在使用rails 3.1,因此您应该更新编写查询的方式:。我正在更新将查询写入.where而不是.find和.all的方式。总的来说,我仍然掌握HABTM查询的窍门。这是我第一次尝试=
has_many :children_users
has_many :parents, :through => :children_users, :class_name => "User", :source => :user