0

我在用戶和孩子之間建立了一個簡單的HABM關係。HABTM搞砸了 - Rails 3

如果孩子屬於你,我希望你看到孩子和這個孩子對象所屬的其他用戶的名字。出於某種原因,我無法打印孩子所屬的其他用戶的姓名。

我選擇這樣:

@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的tiple用戶不會像上面解釋的那樣發生。 如果我使用上面的查詢,即使孩子有多個用戶(我通過做一個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 
+0

的報價在你的'link_to'看起來不對 –

+0

感謝您的緩存。這是一個問題,當我拿出DIV在這裏過去時,因爲這個div與這個問題無關。爲了清晰起見,請將div放回原處。 – Yuri

回答

0

你正在試圖獲得用戶的兒童,並在同一時間,讓所有的其他家長?

一個簡單的方法來列出用戶的孩子並顯示每個孩子的父母會:

@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 
+0

羅賓,你保存了一天。謝謝。我沒有用戶has_many:通過,但你的例子與我目前的HABTM一起工作。我用@children = current_user.children替換'@children = Child.all(:include =>:users,:conditions => [「users.id =?AND enrolled =?」,current_user.id,true]))它只是工作。我應該想到/嘗試過。獎勵! – Yuri

+1

我的榮幸;)另外,這是一個細節,但'如果可以? :管理,Child'部分可以在循環之外:'can_manage_children = can? :manage,Child;'然後在循環中,執行'<%= link_to '[#{user.first_and_last_name}]', can_manage_children ? edit_child_path(child) : new_parentnote_path, :title => "#{user.updateChoice}"%>'。另外,你正在使用rails 3.1,所以你應該更新你寫查詢的方式:)。 – Robin

+0

我正在更新我寫查詢的方式,而不是.find和.all。一般來說,我仍然會遇到HABTM查詢的問題。這是我第一次刺穿它。 =) – Yuri

0

我相信它是使加載只有CURRENT_USER的兒童的狀況"users.id = ? ", current_user.id,隨後當你遍歷那些有唯一的用戶,是當前用戶。

你probbly要基於children.user_id選擇什麼:

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

感謝Clyfe的回覆。但是,我沒有child.user_id列。我試過你的建議,並得到(PGError:錯誤:列的children.user_id不存在)。 Currently in Dev。 db中只有2個孩子。兩者都有多個用戶。我很困惑它爲什麼不起作用。 – Yuri

+0

請提供您的架構的所有細節以及您正在嘗試完成的內容。 – clyfe

+0

添加了我試圖完成的模式和細節。感謝您的幫助Clyfe。 – Yuri