M
M
Max2016-04-22 21:18:22
Ruby on Rails
Max, 2016-04-22 21:18:22

How to access model objects?

Hello, tell me how to deal with calls to model objects. I read the documentation, but I can not find the answer to this question.
I pull the last user in the console - u = User.last
Then I pull all the places from the table (places) that this user booked -u.places

[#<Place id: 3, cinema_id: 1, film_session_id: 1, place_number: 10, status: false, created_at: "2016-04-15 11:02:40", updated_at: "2016-04-15 11:02:40", user_id: 1>, ...

Each location belongs to a specific session (film_session_id). Each session has a name like f = FilmSession.last:
<FilmSession id: 6, cinema_id: 3, session_name: "deadpool", number_of_session_place: 100, created_at: "2016-04-21 12:40:42", updated_at: "2016-04-21 12:40:42", user_id: 1>

The task is this - in the user's profile, display all the places that he has booked. Or rather, a table with the seat number, session name, and so on. I take everything out, but I don’t understand how they get through to the name of the session for which he booked a place.
Update
User Model Relationships
has_many :cinemas
  has_many :film_sessions
  has_many :places

Session Relationships
belongs_to :cinema
  has_many :places, :dependent => :destroy
       belongs_to :user

Place links
has_many :film_sessions
  belongs_to :cinema
  belongs_to :user

And cinema connections
has_many :film_sessions
  belongs_to :user

I solved the problem with this code in the view, but something tells me that this code can be called a bad word
<% @user.places.each do |place| %>
      <tr>
        <td><%= place.place_number %></td>
        <td><%= place.cinema.cinema_name %></td>
        <% p = place.film_session_id %>
        <% f = FilmSession.find(id = p) %>
        <td><%= f.session_name %></td>
      </tr>
    <% end %>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question