Home>

We are making a function to record the current work time.

Table is

#User table
・ Id
・ Username
#WorkingHour table
・ User_id
・ Start_time
・ Finish_time

The id of the User table is linked to the user_id of the WorkingHour table.

In order to display the work time of all users in the administrator's view, I make a table that increases according to the number of tables,

id name Working time
1 User 1 8/11 11: 00-12: 00
8/11 14: 00-18: 00
2 User 2 8/11 11: 00-12: 00
3 User 3 8/11 11: 00-12: 00
8/13 10: 00〜12: 00
It looks like

.

I'm struggling without knowing how to retrieve the data in view at this time.

First on the controller side

@working_hours = WorkingHour.all
      @users = User.all

It ’s all taken out like this

On the View side

% table
    % thead
      % tr
        % th ID
        % th name
        % th working time
      % tr
    % thead
    % tbody
      [email protected] working_hours.each do | working_hour |
        -user = User.where (id: working_hour.user_id) #I don't know if this is right
        % tr
          % td = working_hour.user_id
          % td =
          % td =
        % tr
    % tbody
  % table

I am using haml, but I stopped writing my id until it was displayed in a loop.
What is the best way to get the records where the User table id and the WorkingHour table user_id match in one loop of @working_hours and display the contents of the username column in td? . .

If i do not know if this is correct, it will be an error if you take out user.username as it is, if you write and display just user, something like ActiveRecord :: Relation will be displayed .

  • Answer # 1

      

    -user = User.where (id: working_hour.user_id) #I don't know if this is right

    This uses, so the result isActiveRecord :: Relation. In other words, the result is obtained in the form of an array. Add.firstto get the beginning.

    That is

    -user = User.where (id: working_hour.user_id) .first


    I think that there will be no problem.

    AlsoUser.where (id: 1) .firstThis pattern isUser.find (1)orUser.find_by (id: 1)< You can also write/code>. It is better to usefindorfind_byto get one record with primary_key specified.

    Moreover, perhaps on the model side

    class WorkingHour<ApplicationRecord
      belongs_to: user
      ...
    end
    Isn't there a description ofbelongs_to: userlike

    ?
    In this case in View

    -user = working_hour.user


    You should be able to get an instance of the User model.

  • Answer # 2

      

    User table id and WorkingHour table user_id are linked

    As far as you can see,userandworking_hourseem to have a one-to-many relationship in the current model design.
    Then, if you writeuser.working_hours, andworking_hourslinked touser, you should be able to get all records.

      

    Retrieving records where User table id and WorkingHour table user_id match in one loop of working_hours

    However,working_hour.userscannot get hourlyuserin the current model design.

    I don't know the order as shown in the table (by date>by user>by time?), but for such complex arrangements you need a slightly more complex model design Isn't it becoming?
    It seems necessary to prepare at least an intermediate table and makeuserandworking_houra many-to-many relationship.

  • Answer # 3

    (There's a N + 1 problem at first glance.

    -user = User.where (id: working_hour.user_id) #I don't know where this is right

    where,

    -user = User.find_by (id: working_hour.user_id)
    In

    , you will find an instance of User class in user.

    Now

      

    If you take out user.username as it is, an error will occur

    I think

    disappears.