Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Problem with Filling Tables


  • Please log in to reply
4 replies to this topic

#1 distraff

distraff

    Passenger

  • Members
  • 4 posts

Posted 02 September 2013 - 08:13 PM

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


#2 Funnyvibe

Funnyvibe

    Signalman

  • Members
  • 17 posts

Posted 02 September 2013 - 08:28 PM

Stab in the dark but try save! instead of save. Reason being I think you are trying to run queries against active record objects that haven't been written yet.

#3 distraff

distraff

    Passenger

  • Members
  • 4 posts

Posted 02 September 2013 - 08:39 PM

Tried that.  The error is still happening.



#4 Ohm

Ohm

    Guard

  • Members
  • 184 posts
  • LocationCopenhagen

Posted 02 September 2013 - 09:45 PM

On your fourth line in the self.create_styles method you'd probably want s instead of list_styles.

def self.create_styles(list_styles)
  list_styles.each do |s|
    new_style = Style.create(name: s[:name], description: s[:description])
    new_style.save
    
    s[:beers].each do |b| # <= Here I changed it to 's'
      new_style.beers.create(name: b[:name])
      new_style.beers.save
     end
   end
 end

Blog: http://ohm.sh | Twitter: madsohm


#5 Funnyvibe

Funnyvibe

    Signalman

  • Members
  • 17 posts

Posted 02 September 2013 - 10:16 PM

I was able to sit down with my laptop and take a closer look.  I saw two problems.

 

1.  On line 15 you were trying to access an array [:beers] from your list_styles array and not the hash that exists under one of these array items.  Changing it to s[:beers] seemed to correct that, like the above poster mentioned.

 

2. You were not calling save on a specific ActiveRecord object, but an entire array of potential beer objects.  The correct way would to either assign the new object to a name and save it, or to call the save function inline with the create statement i.e new_style.beers.create(name: b[:name]).save.

 

I attached code with these changes that should work.

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
 
       s[:beers].each do |b| #Line 15
         new_beer = new_style.beers.create(name: b[:name])
         new_beer.save
       end
 
     end
   end

  • james likes this




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users