Home>
I want to achieve
In rails6 API modeFrom the users (free_users) registered in the same station, narrow down the stations (active_stations) where there are 6 or more unscheduled users, and form a chat room with unplanned users who have the same station.I'm trying.
errorI will put the error statement first.
Error ①
NoMethodError (undefined method `free'for User (call'User.connection' to establish a connection): Class
Error ②
ActiveRecord :: StatementInvalid (PG :: UndefinedTable: ERROR: missing FROM-clause entry for table "free_users"
Corresponding code
user_rooms_controller (intermediate table that links chat rooms and users)
def create
#This is the place where the error occurred -------------------------------------------------------------------------------
In case of error ① @active_stations = Station.joins (: users) .where ("users.where ({free: true} .count>= 6)")
In case of error ② @ active_stations = Station.joins (: users) .where ("users.where ({free: true} .count>= 6)")
-----------------------------------------
@ active_stations.each do | station | #Processing for each settable station
room = Room.create (station_id: station.id) # 1, create a room for a station that can be set
users = station.users.where (free: true) #Get unplanned users who have registered the same settable station
selected_users = users.sample (6) # Randomly extract 6 people from unplanned users who have registered the same station
selected_users.each do | user | # Treat unplanned users to have the same room id
room.save
user_room = UserRoom.build! (User_id: user.id, room_id: room.id)
user.free = false
user.save
render json: {status:'SUCCESS', message:'Loaded user_rooms', data: user_room}
end
end
end
station.rb (define free_users here)
def free_users
free_users = self.users.where (free: true)
return free_users
end
What I tried
I tampered with the location where the error occurred as follows. I understand that I can't use the separately defined free_users in terms of how to use the where method.
@active_stations = Station.where (user: User.where (free: true))
Thanks for your professor!
-
Answer # 1
Related articles
- ruby - [rails] about the error that occurs when deleting likes (routing error)
- ruby on rails 6 - about rails error "wrong number of arguments (given 1, expected 0)"
- ruby on rails - about the error when trying to implement jquery with rails ($is not defined;please fix or add/* global * /)
- ruby on rails - [rails] about the problem that screen transition is not possible (error does not occur)
- ruby on rails 6 - [error] nomethoderror in followscontroller # destroy undefined method `destroy'for nil: nilclass
- ruby on rails 6 - error resolution for no route matches [delete]
- about ruby error statement
- ruby - about how to save db with rails devise
- ruby on rails 6 - [error] uninitialized constant user (call'userconnection' to establish a connection) :: image
- ruby on rails - i want to improve rails name error
- ruby - about the delete function in rails
- ruby - the error in rails g migration cannot be resolved
- ruby on rails - about rails create action
- ruby on rails 6 - [error] nomethoderror in communitiescontroller # create undefined method `published?'for # <community: 0x0
- ruby on rails 6 - rails tutorial chapter 14 about follow, unfollow, following? methods
- ruby on rails - when i run rails, i get an error related to webpacker
- ruby - an error occurs when building a rails 6 environment with docker
- ruby - about rails routing
- ruby on rails 6 - [error] nomethoderror in communities # index undefined method `following?'for nil: nilclass
Related questions
- ruby - nomethoderror in posts#edit undefined method `content' for nil:nilclass in rails
- ruby - a version of rails that programming novices should learn from now
- ruby on rails 5 - local server of ruby on rails cannot be built
- ruby - best practices for implementing an api that returns json in an existing rails app
- ruby on rails 5 - i want to specify version 5242 when launching a new application with rails
- html - all the show pages of resources will be the same
- javascript - on the rails ec site, each statement modifies all products
- ruby - [rails] error when trying to create order model from product_controller
- ruby - error in heroku
... I am also a teacher, but I will post it in the hope that I can help as much as possible.
I hope you don't curse me even if it doesn't move or isn't what I intended.
Wouldn't it fit in a circle if the processing of this case was separated without being forced into one line?
1:
active_station_ids = User.where (free: true) .pluck (station_id) .uniq
Type in the id of the station you want to take out in the array2:
@active_stations = Station.where (id: active_station_ids)
Pull out a list of stations with something likeI don't think it's the best solution, but I'm wondering if it's a little better than getting stuck in a swamp without being able to solve it ...
If it's strange, you can go through it.
PS: The code has been added, but
@active_stations = Station.joins (: users) .include (: users) .where (users: {free: true})
Didn't it work? ?? I think it would be the smartest if this passed ...After all the real world is ruthless.