2011-02-15 102 views
0

使用Rails 2.3.8。我想要顯示來自複雜關係的數據。顯示來自多個相關表格的數據

這些都是在我的模型:

shop.rb

has_many :city_shops 

id | name 
3 | Good Cafe 

city_shop.rb

belongs_to :city 
belongs_to :shop 

id | city_id | shop_id | notes 
2 | 4 | 3 | Delicious food in Paris 

city.rb

belongs_to :article 
has_many :city_shops 
has_many :shops, :through => :city_shops 

id | article_id 
4 | 5 

article.rb

has_many :shops, :through => :shop_articles 
has_many :cities, :dependent => :destroy 

id | user_id | name 
5 | 6 | Favorite shops in France 

user.rb

has_many :articles 

id | login 
6 | victor 

的場景是該(可以不是邏輯的): 與ID 6所述的用戶創建了許多文章。假設他創建了這篇名爲的文章法國最受歡迎的商店。在這篇文章中,有cities。在每個city中,有city_shops,其中有shopnotes的細節從city_shops

我也有個人shop頁面。我希望訪問者知道用戶留下了什麼筆記,筆記的位置以及該用戶文章的鏈接。

shop頁面,它應該閱讀:

說明用戶 「美味食品在巴黎」的「勝利者」,「鏈接,在法國最喜歡的商店」共享。

我希望這個問題比以前更清楚。非常感謝你。

+0

所以,你必須@shop並希望從關聯模型顯示的數據? – LapinLove404 2011-02-15 17:24:46

+0

閱讀本指南:http://guides.rubyonrails.org/association_basics.html應該幫助你管理協會。 – tjeden 2011-02-15 20:02:26

+0

我修改了我的問題並提供了更多詳細信息。你能再看一遍嗎?謝謝! – Victor 2011-02-16 06:15:29

回答

1
Notes by users:<br /> 
<% @shop.city_shops.each do |city_shop| %> 
    "<%= city_shop.notes %>" by "<%= city_shop.city.article.user.login%>", shared in <%= link_to city_shop.city.article.name, "#{url}" %><br /> 
<% end > 

你必須提供鏈接的URL,你必須也添加到您的文章型號:

belongs_to :user 
1
<% @shop.city_shops.each do |city_shop| %> 
    city_shops notes: <%= city_shop.notes %> 
    users: <%= city_shop.city.country.user.username %> 
    country_id: <%= city_shop.city.country_id %> 
<% end %> 

你的模型有些奇怪的東西。