Jack Jack - 2 months ago 6
Ruby Question

rake db:seed not inputting all of my data

I am having issues of my

rake db:seed
not being applied to the correct values when I attempt to seed. I also get no error when I
rake db:seed
which is why I am unsure what is wrong.

Here are the migration files:
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


Boosts:

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


Here is a sample of my seed file:

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)


I am unsure what I'm doing wrong since there is no seeding error given it just passes onto the next bash line with no message.

EDIT
Models with validation

class Boostdet < ApplicationRecord
belongs_to :boosts
end

class Boost < ApplicationRecord
has_many :boostdets
end

Answer

Create boostdets using the association. This way you don't need to manually pass the 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: "")

One more thing you need to change the association belongs_to :boosts to belongs_to :boost

class Boostdet < ApplicationRecord
  belongs_to :boost
end