在 ruby 和 rails 上使用嵌套属性播种后 ID 没有关联?
Id is not associated after seeding with nested attributes on ruby on rails?
为什么我播种后space_listing_id
是空的?
我的模型上有这个。
class SpaceListing < ActiveRecord::Base
belongs_to :user
has_many :images, dependent: :destroy
has_many :bookings, dependent: :destroy
has_many :favorites
has_many :reviews, dependent: :destroy
accepts_nested_attributes_for :images, allow_destroy: true
validates :title, presence: true
validates :description, presence: true
validates :day_rent, presence: true
validates :monthly_rent, presence: true
validates :space_type, presence: true
validates :environment_type, presence: true
validates :size_length, presence: true
validates :size_width, presence: true
validates :size_height, presence: true
end
这是我的 seed.rb
拥有的:
SpaceListing.create!([
{
user_id: 1,
title: "Hipster Vest",
description: "Hipster's favorite spot",
street_number: "10",
route: "Hipster Street",
city: "San Francisco",
state: "CA",
zip_code: "94108",
country: "United States",
space_type: "Garage",
environment_type: "Outdoor",
monthly_rent: 100,
day_rent: 3,
size_width: 10,
size_height: 10,
size_length: 10,
latitude: 37.7875474,
longitude: -122.4049517,
images_attributes: [
{image_url: "https://media.giphy.com/media/26BRwxtVBt8dWEuGs/giphy.gif",
space_listing_id: },
{image_url: "https://media.giphy.com/media/l3UcgEIw80eyb36kU/giphy.gif"}
]
}
])
accepts_nested_attributes
创建一个可以传递到 ActiveRecord 方法中的新属性,例如 .create 和 .update。在您的情况下,该属性将被称为 :images_attributes
.
下面的代码将解决您的问题。
SpaceListing.create!([
{
user_id: 1,
title: "Hipster Vest",
description: "Hipster's favorite spot",
street_number: "10",
route: "Hipster Street",
city: "San Francisco",
state: "CA",
zip_code: "94108",
country: "United States",
space_type: "Garage",
environment_type: "Outdoor",
monthly_rent: 100,
day_rent: 3,
size_width: 10,
size_height: 10,
size_length: 10,
latitude: 37.7875474,
longitude: -122.4049517,
images_attributes: [
{ image_url: "https://media.giphy.com/media/26BRwxtVBt8dWEuGs/giphy.gif"},
{ image_url: "https://media.giphy.com/media/l3UcgEIw80eyb36kU/giphy.gif"}
]
}
])
更新:
您似乎已经更新了问题。我会回答的。您不需要在属性散列中包含 :space_listing_id
。使用我上面发布的代码,Rails 将自动关联记录并填充 :space_listing_id
键。
为什么我播种后space_listing_id
是空的?
我的模型上有这个。
class SpaceListing < ActiveRecord::Base
belongs_to :user
has_many :images, dependent: :destroy
has_many :bookings, dependent: :destroy
has_many :favorites
has_many :reviews, dependent: :destroy
accepts_nested_attributes_for :images, allow_destroy: true
validates :title, presence: true
validates :description, presence: true
validates :day_rent, presence: true
validates :monthly_rent, presence: true
validates :space_type, presence: true
validates :environment_type, presence: true
validates :size_length, presence: true
validates :size_width, presence: true
validates :size_height, presence: true
end
这是我的 seed.rb
拥有的:
SpaceListing.create!([
{
user_id: 1,
title: "Hipster Vest",
description: "Hipster's favorite spot",
street_number: "10",
route: "Hipster Street",
city: "San Francisco",
state: "CA",
zip_code: "94108",
country: "United States",
space_type: "Garage",
environment_type: "Outdoor",
monthly_rent: 100,
day_rent: 3,
size_width: 10,
size_height: 10,
size_length: 10,
latitude: 37.7875474,
longitude: -122.4049517,
images_attributes: [
{image_url: "https://media.giphy.com/media/26BRwxtVBt8dWEuGs/giphy.gif",
space_listing_id: },
{image_url: "https://media.giphy.com/media/l3UcgEIw80eyb36kU/giphy.gif"}
]
}
])
accepts_nested_attributes
创建一个可以传递到 ActiveRecord 方法中的新属性,例如 .create 和 .update。在您的情况下,该属性将被称为 :images_attributes
.
下面的代码将解决您的问题。
SpaceListing.create!([
{
user_id: 1,
title: "Hipster Vest",
description: "Hipster's favorite spot",
street_number: "10",
route: "Hipster Street",
city: "San Francisco",
state: "CA",
zip_code: "94108",
country: "United States",
space_type: "Garage",
environment_type: "Outdoor",
monthly_rent: 100,
day_rent: 3,
size_width: 10,
size_height: 10,
size_length: 10,
latitude: 37.7875474,
longitude: -122.4049517,
images_attributes: [
{ image_url: "https://media.giphy.com/media/26BRwxtVBt8dWEuGs/giphy.gif"},
{ image_url: "https://media.giphy.com/media/l3UcgEIw80eyb36kU/giphy.gif"}
]
}
])
更新:
您似乎已经更新了问题。我会回答的。您不需要在属性散列中包含 :space_listing_id
。使用我上面发布的代码,Rails 将自动关联记录并填充 :space_listing_id
键。