ruby 播种用户订餐台

clj7thdc  于 2023-02-21  发布在  Ruby
关注(0)|答案(2)|浏览(110)

Seeding Database tables Seeding user data.... Seeding meals data .... Seeding user meal booking data .... rails aborted! NoMethodError: undefined method用户订餐'对于nil:NilClass

假设用户的电子邮件地址为“user@example.com“

meals = Meal.all
    user = User.find_by(email_address`your text`: "user@example.com")
    userbooking1 = user.UserMealBooking.create!(meal_id: meals.first.id, booking_type: "online", booking_date: "2023-05-12T10:00")

我希望当一个用户预订一餐他们的id可以显示或电子邮件'你的文字'

ffx8fchx

ffx8fchx1#

你不能从一个对象调用一个类,这看起来不对
我假设UserMealBooking是用户和膳食之间的模型,因此有多种创建关联的方法,其中之一是

meals = Meal.all
    user = User.find_by(email_address`your text`: "user@example.com")
    userbooking1 = UserMealBooking.create!(meal_id: meals.first.id, user_id: user.id, booking_type: "online", booking_date: "2023-05-12T10:00")

确保您的数据库中有餐食和具有特定电子邮件的用户

yyyllmsg

yyyllmsg2#

我假设您的User模型与UserMealBooking模型有一个has_many关联,在这种情况下,您可以修改代码如下:

meals = Meal.all
user = User.find_by(email_address`your text`: "user@example.com")
userbooking1 = user.user_meal_bookings.create!(meal_id: meals.first.id, booking_type: "online", booking_date: "2023-05-12T10:00")

相关问题