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)
  end

   def self.create_styles(list_styles)
     list_styles.each do |s|
       new_style = Style.create(name: s[:name], description:
s[:description])
       new_style.save
       list_styles[:beers].each do |b|
         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:in
`block 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:in
`create_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:in
`block 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:in
`load'
/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:in
`load_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:in
`eval'
/home/danberghofer/.rvm/gems/ruby-2.0.0-p247/bin/ruby_noexec_wrapper:14:in
`<main>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)

-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/8132b1a24dd64ea4024a6c068449e4dc%40ruby-forum.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to