Problem with Filling Tables

I have an application that has models for beer styles.

Every beer style will have a name and description. It will also contain
a list of beer models.

Every beer model will contain a name.

I want my application to fill in the list of styles with some default
data.

#Beer migration file:
class CreateBeers < ActiveRecord::Migration
def change
create_table :beers do |t|
t.string :name
t.references :style
t.timestamps
end
add_index :beers, :style_id
end
end

#Style migration file:
class CreateStyles < ActiveRecord::Migration
def change
create_table :styles do |t|
t.string :name
t.text :description
t.timestamps
end
end
end

#Beer Model
class Beer < ActiveRecord::Base
belongs_to :style
end

#Style Model
class Style < ActiveRecord::Base
has_many :beers
def self.makeStyles
beer = Hash[:name => “individual beer”]
beerArray = [beer]
style = Hash[:beers => beerArray, :name => “beer style”,
:description => “This is a beer style.”]
styleArray = [style]
self.create_styles(styleArray) #Line 8
end

def self.create_styles(list_styles)
list_styles.each do |s| #Line 12
new_style = Style.create(name: s[:name], description:
s[:description])
new_style.save
list_styles[:beers].each do |b| #Line 15.
new_style.beers.create(name: b[:name])
new_style.beers.save
end
end
end
end

The parts in comments is the code that was breaking the application.

When I ran rake db:seed on the code without the comments, I got the
following error message. Does anyone know what is going on?

hererake aborted!
no implicit conversion of Symbol into Integer
/home/danberghofer/Documents/cpts421/lions/trunk/lionsBeers/app/models/style.rb:15:in
[]' /home/danberghofer/Documents/cpts421/lions/trunk/lionsBeers/app/models/style.rb:15:inblock in create_styles’
/home/danberghofer/Documents/cpts421/lions/trunk/lionsBeers/app/models/style.rb:12:in
each' /home/danberghofer/Documents/cpts421/lions/trunk/lionsBeers/app/models/style.rb:12:increate_styles’
/home/danberghofer/Documents/cpts421/lions/trunk/lionsBeers/app/models/style.rb:8:in
makeStyles' /home/danberghofer/Documents/cpts421/lions/trunk/lionsBeers/db/seeds.rb:9:in<top (required)>’
/home/danberghofer/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:222:in
load' /home/danberghofer/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:222:inblock in load’
/home/danberghofer/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:213:in
load_dependency' /home/danberghofer/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:222:inload’
/home/danberghofer/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/engine.rb:540:in
load_seed' /home/danberghofer/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/tasks/database_tasks.rb:153:inload_seed’
/home/danberghofer/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/railties/databases.rake:181:in
block (2 levels) in <top (required)>' /home/danberghofer/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:ineval’
/home/danberghofer/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in
`’
Tasks: TOP => db:seed
(See full trace by running task with --trace)

On 2 September 2013 21:10, Dan D. [email protected] wrote:


beer = Hash[:name => “individual beer”]

That should be beer = {:name => “individual beer”} or beer =
HASH[“name”, “individual beer”]
See Class: Hash (Ruby 2.0.0)

style = Hash[:beers => beerArray, :name => "beer style",

:description => “This is a beer style.”]

Same again

Colin