0

I have this model with an array of strings as one of the columns

  create_table "shows", force: :cascade do |t|
    t.time "st"
    t.time "ed"
    t.integer "money"
    t.string "av_seats", default: "--- []\n"
    t.string "oc_seats", default: "--- []\n"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.integer "theatre_id"
    t.integer "screen_id"
    t.integer "movie_id"
    t.index ["movie_id"], name: "index_shows_on_movie_id"
    t.index ["screen_id"], name: "index_shows_on_screen_id"
    t.index ["theatre_id"], name: "index_shows_on_theatre_id"
  end

I'm trying to reference av_seats and oc_seats in another model's controller via this logic

def create
    @booking = Booking.new(booking_params)
    @av_seats = Show.find(params[:show_id]).pluck(:av_seats)
    @oc_seats = Show.find(params[:show_id]).pluck(:oc_seats)

    if @booking.save
      if @av_seats.include? @booking.seat
        @oc_seats << @booking.seat
        @av_seats.delete @booking.seat
      end
      render json: @booking, status: :created, location: @booking

Essentially, it's supposed to move an element from one array to another and delete the same element from the previous array all based on input.

"status": 404,
    "error": "Not Found",
    "exception": "#<ActiveRecord::RecordNotFound: Couldn't find Show without an ID>",

I've tried using the pluck method beforehand and then attempt to use find for the element but I may not be doing it right either.

    @av = Show.pluck(:show_id, :av_seats)
    @oc = Show.pluck(:show_id, :oc_seats)

    @av_seats = Show.find(@av)
    @oc_seats = Show.find(@oc)
# routes.rb

Rails.application.routes.draw do
  resources :movies
  resources :bookings
  resources :shows
  resources :screens
  resources :theatres
  resources :users
end

This is what the form typically looks like. It's an API so no views, just pure JSON.

{
    "booking": {
        "user_id": 1,
        "show_id": 2,
        "screen_id": 2,
        "seat": "A5"
    }
}
4
  • = Show.find(params[:id]) Commented Mar 22, 2022 at 20:55
  • If you're getting Couldn't find Show without an ID is because nothing comes in params[:show_id]. Commented Mar 22, 2022 at 21:11
  • can you show the contents of params, contents of routes.rb and the form that leads to the create action please. Commented Mar 22, 2022 at 22:31
  • What do you contents of params? Like the ids? @Pascal Commented Mar 23, 2022 at 4:55

1 Answer 1

1

You create a Booking resource. The information (:show_id) is nested under the :bookings key in your params

I assume you got the following method on the BookingsController

def booking_params
  params.require(:booking).permit(:show_id, :user_id, :seat, :screen_id)
end

?

If your Booking has the association setup properly

class Booking < ApplicationRecord
  belongs_to :show
end

Then you can access the show like this:

booking = Booking.new(booking_params)
show = booking.show

If no association is setup, then you can use

show = Show.find(params[:booking][:show_id])
# or
show = Show.find(booking_params[:show_id]) # not _params_

Since the show object is loaded anyhow, you can just access av_seats and oc_seats directly

show.av_seats
show.oc_seats

pluck is an optimization where only the columns you pluck are fetched from the DB.

You probably want to change the xx_seats columns to be of type string[] (with array: true in your migration).

Once you change the contents of the seats array(s) you need to save the show record.

Sign up to request clarification or add additional context in comments.

3 Comments

Oh nice, yeah, I see what you mean. I managed to get it to finally resolve the error and it copies the characters just fine. But it doesn't delete it from oc_seats for some reason. Also, they already are strings, sqlite3 is just weird with how it does array of strings and it took some finnicking around.
Ah, sqlite does not support array types. So You might need to serialize and deserialize your array on your own. I don't use sqlite so not sure what the common way is though.
I wouldn't worry about that, I figured that out before having this issue haha. In any case, I appreciate the help. I've managed to solve the error. It wasn't saving show afterwards so the effects wouldn't take place so I added a show.save to the logic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.