0

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 cities. 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.

3
  • So, you have @shop and want to display data from associated models ? Commented Feb 15, 2011 at 17:24
  • Reading this guide: guides.rubyonrails.org/association_basics.html should help you undertand associations. Commented Feb 15, 2011 at 20:02
  • I have revised my question and given more details. Can you look at it again? Thanks! Commented Feb 16, 2011 at 6:15

2 Answers 2

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 >

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

belongs_to :user
Sign up to request clarification or add additional context in comments.

2 Comments

Value of <%= city_shop.city.article.user.login%> or any plain text after <%= city_shop.notes %> will be shown twice somehow. Any reason why?
hmm...I cant think of any reason..can you try city_shop.inspect to understand what is going on..
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 %> 

There is something strange with your models.

1 Comment

I have given more details on the question. I hope it's clearer now. Can you please read through again? Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.