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
- i want to retrieve the element of my group with ruby on rails
- ruby on rails - the capacity shortage of cloud 9 and ec2 cannot be solved
- ruby - [rails] resolve nameerror in items#new and want to be able to save image data
- ruby on rails 5 - argumenterror on scope
- ruby - the formhelper module set by rails is not loaded
- ruby on rails: gem install sqlite3 command does not pass
- ruby - rails nomethoderror cannot be resolved
- ruby - rails server can't start
- ruby - i want to start a rails server
- ruby on rails - why is the root directory in scss path specification not "~/"?
- ruby on rails - in rails command, undefined method `application' for rails:module (nomethoderror) comes out
- ruby on rails ruby calculation method description method
- ruby on rails - rils i don't want to make transitions by hand (you will be redirected before entering the edit screen)
- ruby - rails email sending error doesn't go away
- ruby - i want to handle exceptions using rails rescue
- ruby on rails - i want to hit api with curl
- ruby on rails 5 - rubyonrails tutorial chapter 10 assert error
- ruby on rails - undefined method `' for nil:nilclass in rake assets:precompile after deploy is executed
- ruby - when editing the rails profile, only the comments are not reflected
- ruby on rails 5 - rails association about one-to-many association
- ruby - images in assets files are not displayed correctly in the rails app
- ruby - not saved by create action
- ruby - [rails] i want to register data to multiple models with one form_with
- ruby - [rails] i want to use fields_for with form_with, but an error occurs and the page cannot be displayed
- ruby - render function is not reflected well haml usage
- ruby - △△ how to write haml
- i want to display the list of posted images normally in ruby!
- ruby - like nomethoderror that occurred after implementing the function
- ruby - scss is not reflected
- ruby - group_id cannot be obtained