【问题标题】:HABTM messing me up - Rails 3HABTM 把我搞砸了 - Rails 3
【发布时间】:2011-12-11 11:44:01
【问题描述】:

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

如果孩子属于您,我希望您看到孩子以及该孩子对象所属的其他用户的姓名。由于某种原因,我无法打印孩子所属的其他用户的姓名。

我这样选择:

@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%>

它选择正确的孩子,但不打印其他用户的名字。只有当前用户的名字。

如果我选择所有孩子

Child.all
一切都按预期工作。所有的名字都被打印出来,这告诉我不是我的身份验证系统在做一些可疑的事情,而是在做其他事情……可能是我选择孩子的方式,尽管我尝试了几种方法 =(

我不确定我是否遗漏了一些非常明显的东西,但这让我困惑了好几个小时。

非常感谢任何帮助。谢谢。

编辑 - 添加架构

按照下面的要求,这里是架构。据我所知,这种关系建立得很好。它只是根据 current_user 获得多个用户,而如上所述并没有发生。 如果我使用上面的查询,即使孩子有多个用户(我通过执行 child.count 看到)它只会打印当前用户的姓名,因为某种原因它不会获取孩子所属的其他用户名也一样。

  create_table "children_users", :id => false, :force => true do |t|
    t.integer "child_id"
    t.integer "user_id"
  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

【问题讨论】:

  • link_to 中的引号看起来不对...
  • 感谢缓存。这是一个问题,当我拿出 DIV 在这里过去时,因为 div 与问题无关。为了清楚起见,将 div 放回原处。

标签: ruby-on-rails activerecord ruby-on-rails-3.1 has-and-belongs-to-many


【解决方案1】:

您试图获取用户的孩子,同时获取所有其他父母?

列出用户的孩子并显示每个孩子的父母的简单方法是:

@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 %>

ps:

在儿童模型中

has_many :children_users
has_many :parents, :through => :children_users, :class_name => "User", :source => :user

【讨论】:

  • 罗宾,你拯救了这一天。谢谢你。我没有使用 has_many :through,但您的示例适用于我当前的 HABTM。我用@children = current_user.children 替换了@children = Child.all(:include =&gt; :users, :conditions =&gt; ["users.id = ? AND enrolled = ?", current_user.id, true]),它就起作用了。我应该想到/尝试过。赞一个!
  • 我的荣幸 ;) 此外,这是一个细节,但 if can? :manage, Child 部分可能在循环之外:can_manage_children = can? :manage, Child; 然后在循环中,只需执行 &lt;a rel='tipsy'&gt;&lt;%= link_to '[#{user.first_and_last_name}]', can_manage_children ? edit_child_path(child) : new_parentnote_path, :title =&gt; "#{user.updateChoice}"%&gt;&lt;/a&gt;。此外,您使用的是 rails 3.1,因此您应该更新编写查询的方式:)。
  • 我正在更新将查询写入 .where 而不是 .find 和 .all 的方式。一般来说,我仍然掌握 HABTM 查询的窍门。这是我第一次尝试它。 =)
【解决方案2】:

我相信是 "users.id = ? ", current_user.id 的条件使得只加载 current_user 的子项,随后当您迭代那些唯一的用户时,那里的唯一用户就是当前用户。

您可能想要的是根据children.user_id 选择:

@children = Child.where(user_id: current_user.id, enrolled: true).includes(:users)

【讨论】:

  • 感谢克莱夫的回复。但是,我没有 child.user_id 列。我尝试了您的建议并得到了(PGError:错误:列 children.user_id 不存在)。目前在开发。数据库中只有 2 个孩子。两者都有多个用户。我很困惑为什么它不起作用。
  • 请提供有关您的架构的所有详细信息以及您要完成的工作。
  • 添加了我想要完成的架构和详细信息。感谢您的帮助克莱夫。
猜你喜欢
  • 1970-01-01
  • 2012-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-24
  • 1970-01-01
相关资源
最近更新 更多