开发者

Showing data from multiple related tables

开发者 https://www.devze.com 2023-02-10 11:19 出处:网络
Using Rails 2.3.8. I wanna show data from complicated relationship. These are in my models: shop.rb has_many :city_shops

Using Rails 2.3.8. I wanna show data from complicated relationship.

These are in my models:

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

The scenario is this (may not be logical): A user with ID 6 creates many articles. Let's say he creates this article called Favorite shops in France. In this article, there are c开发者_JAVA百科ities. In each city, there are city_shops where in it there are details shop and notes from city_shops.

I also have individual shop page. I want the visitors to know what notes have been left by users, where the note is and the link to that user's article.

In the shop page, it should read:

Notes by users "Delicious food in Paris" by "victor", shared in "link to favorite shops in France".

I hope this question is clearer than previous. Thank you very much.


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 >

You have to provide the url in the link and you have to add this also to your Article model:

belongs_to :user


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

There is something strange with your models.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号