2011-02-15 42 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 %> 

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

相关问题