views:

98

answers:

2

Hi,

I have designed an application consisting of 20+ tables, and utilizing Ruby on Rails. I am using an authentication system for users to login. I want to display to the user, when he logs in, information related to him that can be found across many tables, between these tables there are all the possible relationships. How can I accomplish this in RoR? Should I consider using views, multiple joins? I did not managed to get information from one model further than 2 tables looking in the tables design using classical approaches like "has many", ":through" etc.

Can you please share your knowledge?

Thanks!

A: 

I assume that your User model has relationships with all the tables that contain related information.

Assuming that, you can have an action in UsersController and a corresponding view. In the action you can find out who is the currently connected user, and then get all the data you need from the related models. Return these data to the view and render them as you like.

Petros
A: 

I think your question is too general, but here are some thoughts...assuming you are making a conventional Rails app

"Should I consider using views?"

Views are what users see in conventional rails apps. So if you want to display data to your users, use views to display data that is made available in the controllers. Each action in the controller will grab data from the models, and have a corresponding view.

So to display books that belong to a user, you might have a Books controller. After a user logs in, you have an "index" action in the "Books" controller that says @user_books = @user.books. In this case, @user would be the user object from your authentication system. A user would have many books, and it is possible that a book would have many users. Then in your views/books/index.html.erb file you have something like:

<ul>
<% @user_books.each do |b| %>
  <li> <%= b.name %> </li>
<% end %>
</ul>

to print a list of book names.

Joins are done automagically by Rails, so you don't have to worry about it quite yet. But if your app gets really big and queries are complicated, you may have to rewrite the SQL statements to improve scalability....but that is obviously a good problem to have.

Hope this helps!

Tony