不能使用实例变量 return 字符串数组中的数组元素
Can't return array elements from array of string using instance variables
我有这个模型,其中一列是字符串数组
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
我正在尝试通过此逻辑在另一个模型的控制器中引用 av_seats
和 oc_seats
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
本质上,它应该将一个元素从一个数组移动到另一个数组,并根据输入从前一个数组中删除相同的元素。
"status": 404,
"error": "Not Found",
"exception": "#<ActiveRecord::RecordNotFound: Couldn't find Show without an ID>",
我试过事先使用 pluck
方法,然后尝试对元素使用 find
,但我可能也没有做对。
@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
这是表格通常的样子。这是一个 API,所以没有观看次数,只有 JSON.
{
"booking": {
"user_id": 1,
"show_id": 2,
"screen_id": 2,
"seat": "A5"
}
}
您创建了一个 Booking
资源。
信息 (:show_id) 嵌套在 params
中的 :bookings
键下
我假设您在 BookingsController
上获得了以下方法
def booking_params
params.require(:booking).permit(:show_id, :user_id, :seat, :screen_id)
end
?
如果您的 Booking
关联设置正确
class Booking < ApplicationRecord
belongs_to :show
end
那么您可以这样访问节目:
booking = Booking.new(booking_params)
show = booking.show
如果没有设置关联,则可以使用
show = Show.find(params[:booking][:show_id])
# or
show = Show.find(booking_params[:show_id]) # not _params_
既然 show 对象无论如何都被加载了,你可以直接访问 av_seats
和 oc_seats
show.av_seats
show.oc_seats
pluck
是一种优化,其中只有您 pluck 的列会从数据库中获取。
您可能希望将 xx_seats
列更改为 string[]
类型(迁移中包含 array: true
)。
更改座位数组的内容后,您需要保存演出记录。
我有这个模型,其中一列是字符串数组
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
我正在尝试通过此逻辑在另一个模型的控制器中引用 av_seats
和 oc_seats
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
本质上,它应该将一个元素从一个数组移动到另一个数组,并根据输入从前一个数组中删除相同的元素。
"status": 404,
"error": "Not Found",
"exception": "#<ActiveRecord::RecordNotFound: Couldn't find Show without an ID>",
我试过事先使用 pluck
方法,然后尝试对元素使用 find
,但我可能也没有做对。
@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
这是表格通常的样子。这是一个 API,所以没有观看次数,只有 JSON.
{
"booking": {
"user_id": 1,
"show_id": 2,
"screen_id": 2,
"seat": "A5"
}
}
您创建了一个 Booking
资源。
信息 (:show_id) 嵌套在 params
:bookings
键下
我假设您在 BookingsController
def booking_params
params.require(:booking).permit(:show_id, :user_id, :seat, :screen_id)
end
?
如果您的 Booking
关联设置正确
class Booking < ApplicationRecord
belongs_to :show
end
那么您可以这样访问节目:
booking = Booking.new(booking_params)
show = booking.show
如果没有设置关联,则可以使用
show = Show.find(params[:booking][:show_id])
# or
show = Show.find(booking_params[:show_id]) # not _params_
既然 show 对象无论如何都被加载了,你可以直接访问 av_seats
和 oc_seats
show.av_seats
show.oc_seats
pluck
是一种优化,其中只有您 pluck 的列会从数据库中获取。
您可能希望将 xx_seats
列更改为 string[]
类型(迁移中包含 array: true
)。
更改座位数组的内容后,您需要保存演出记录。