We are making a function to record the current work time.
#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,
|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|
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
, so the result is
ActiveRecord :: Relation. In other words, the result is obtained in the form of an array. Add
.firstto get the beginning.
-user = User.where (id: working_hour.user_id) .first
I think that there will be no problem.
User.where (id: 1) .firstThis pattern is
User.find_by (id: 1)< You can also write/code>. It is better to use
find_byto get one record with primary_key specified.
Moreover, perhaps on the model side
Isn't there a description of
class WorkingHour<ApplicationRecord belongs_to: user ... end
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,
working_hourseem to have a one-to-many relationship in the current model design.
Then, if you write
user, 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
working_hour.userscannot get hourly
userin 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 make
working_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
-user = User.find_by (id: working_hour.user_id)
, you will find an instance of User class in user.
If you take out user.username as it is, an error will occur
- ruby on rails - [novice] rails wants to retrieve and display columns of specific records in other tables from the db
- ruby on rails - i want to perform asynchronous communication with jquery on rails
- i want to solve the disconnection of the local host of ruby on rails
- ruby on rails - "mysql2 :: error :: connectionerror: ~" occurs during ec2 deployment
- ruby on rails - long error occurs when running rails server (environment construction)
- ruby - rails cannot read scss files other than applicationcss
- ruby - how to write rails raw sql
- ruby on rails - (rails) like feature, asynchronous communication is not reflected
- ruby on rails - i want to create a post form and a post button
- ruby on rails - how to update the data displayed by inner join newly?
- ruby on rails - is this ok for the procedure to add a new rails update method?
- ruby on rails - how to automatically embed the youtube video url posted on rails and display it (i want to store the url in para
- ruby on rails - records are not displayed correctly
- ruby on rails - save is not done during wizard type implementation
- ruby on rails - asynchronous communication does not work
- ruby on rails - how to improve automatic line break of each item in rails view?
- ruby on rails - get individual id by routing
- ruby on rails - rails dbconsole doesn't work with rails
- ruby on rails - rails tutorial 21 heroku deployment the page you were looking for doesn't exist
- ruby on rails - when updating user information, i get a "no route matches [post]"/users/1 "" routing error
- ruby - update method product editing function flea app
- ruby - [rails] i want to resolve the error so that i can transition from the index to the edit page
- ruby - i'm having trouble with the message sending function
- ruby - [rails] i want to be able to resolve errors and display search results
- login function cannot be implemented well with ruby on rails
- ruby - nomethod error [title]
- ruby - i want to implement the ranking function
- i want to make a new branch with rubymine
- ruby - i'm getting an uninitialized constant applicationcontroller::standerderror and i don't know what code is causing the prob
- ruby - i want to display the element's post time created_at as oo hours ago in rails