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.


I 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
        user_room = UserRoom.build! (User_id: user.id, room_id: room.id)
        user.free = false
        render json: {status:'SUCCESS', message:'Loaded user_rooms', data: user_room}

station.rb (define free_users here)

def free_users
    free_users = self.users.where (free: true)
    return free_users
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

    ... 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) .uniqType in the id of the station you want to take out in the array
    2:@active_stations = Station.where (id: active_station_ids)Pull out a list of stations with something like

    I 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.