当我尝试播种时,我遇到了rake db:seed
未应用于正确值的问题。我rake db:seed
时也没有错误,这就是我不确定错误的原因。
以下是迁移文件: Boostdets:
class CreateBoostdets < ActiveRecord::Migration[5.0]
def change
create_table :boostdets do |t|
t.string :name
t.string :title
t.string :slogan
t.string :point1
t.string :point2
t.string :rating
t.string :timeframe
t.string :price
t.string :image
t.references :boost
t.timestamps
end
end
end
提升:
class CreateBoosts < ActiveRecord::Migration[5.0]
def change
create_table :boosts do |t|
t.string :title
t.string :slogan
t.string :rating
t.string :bonus
t.string :image
t.timestamps
end
end
end
以下是我的种子文件示例:
b1 = Boost.create(title: "gg", slogan: "Ej", rating: "u", bonus: "y", image: "")
Boostdet.create(name: "t", title: "u", slogan: "y", point1: "t", point2: "rs", rating: "f",timeframe: "r", price: "Ss", image: "", boost_id: b1.id)
Boostdet.create(name: "g", title: "s", slogan: "x", point1: "e", point2: "Cg", rating: "3", timeframe: "g", price: "t", image: "", boost_id: b1.id)
我不确定我做错了什么,因为没有播种错误,因为它只是传递到下一个没有消息的bash行。
修改 带验证的模型
class Boostdet < ApplicationRecord
belongs_to :boosts
end
class Boost < ApplicationRecord
has_many :boostdets
end
答案 0 :(得分:3)
使用关联创建boostdets
。这样您就不需要手动传递boost_id
boost = Boost.create(title: "gg", slogan: "Ej", rating: "u", bonus: "y", image: "")
boost.boostdets.create(name: "t", title: "u", slogan: "y", point1: "t", point2: "rs", rating: "f",timeframe: "r", price: "Ss", image: "")
boost.boostdets.create(name: "g", title: "s", slogan: "x", point1: "e", point2: "Cg", rating: "3", timeframe: "g", price: "t", image: "")
还需要将关联belongs_to :boosts
更改为belongs_to :boost
class Boostdet < ApplicationRecord
belongs_to :boost
end
答案 1 :(得分:0)
试试这个:
b1 = Boost.create(title: "gg", slogan: "Ej", rating: "u", bonus: "y", image: "")
b1.boostdets.create(name: "t", title: "u", slogan: "y", point1: "t", point2: "rs", rating: "f",timeframe: "r", price: "Ss", image: "")
b1.boostdets.create(name: "g", title: "s", slogan: "x", point1: "e", point2: "Cg", rating: "3", timeframe: "g", price: "t", image: "")
请注意,如果您使用的是belongs_to
,那么它应该是单数的,而不是:
belongs_to :boosts
它应该是:
belongs_to :boost
答案 2 :(得分:-1)
您能否使用rails中的build命令创建boostdet。请检查以下代码,
@boost = Boost.create(title: "gg", slogan: "Ej", rating: "u", bonus: "y", image: "")
@boost.create_boostdet(name: "t", title: "u", slogan: "y", point1: "t", point2: "rs", rating: "f",timeframe: "r", price: "Ss", image: "")
@boost.create_boostdet(name: "g", title: "s", slogan: "x", point1: "e", point2: "Cg", rating: "3", timeframe: "g", price: "t", image: "")