Trouble inserting rows


#1

hey all,

i’m a newbie to ruby & rails -> apologies in advance for the length of
this post &/or anything particularly lame about it :wink:

i’m trying to grab form POST data that’s packaged in a hash of hashes…

<!-- isactive -->
<input type=hidden
       id="song[<%= file[ :seq ] %>][isactive]"
       name="song[<%= file[ :seq ] %>][isactive]"
       value="1">
 </input>

 ( ... several more similar inputs ... )

<!-- time (sec) -->
<input type=text
       id="song[<%= file[ :seq ] %>][time_sec]"
       name="song[<%= file[ :seq ] %>][time_sec]"
       size="2" maxlength="2">
</input>

…then iterate over the hashes in order to write 1 row to mysql for
each “inner” hash (i.e., each song, in this case 2 songs)…

class SongController < ApplicationController

scaffold :song

def addsave

    # iterate over params hash
    @params.each do |songs|

        # iterate over song hash
        songs.each do |song|

            @a_song = Song.new (
                :isactive => song[ :isactive ] ,
                :id_artist => song[ :id_artist ] ,
                :id_genre => song[ :id_genre ] ,
                :id_record => song[ :id_record ] ,
                :seq => song[ :seq ] ,
                :title => song[ :title ] ,
                :time_min => song[ :time_min ] ,
                :time_sec => song[ :time_sec ]
            )

            @a_song.save
        end
    end
end

the good news is that i don’t get any errors; the bad news is that i get
(a) no rows inserted into mysql, and (b) the following output in
development.log …

Processing SongController#addautosave (for 66.92.241.2 at 2006-02-08
16:32:12) [POST]
Parameters: {“action”=>“addautosave”, “controller”=>“song”,
“song”=>{“1”=>{“time_min”=>“1”, “title”=>“E-Pro”, “id_genre”=>“1”,
“id_record”=>“6”, “id_artist”=>“7”, “isactive”=>“1”, “seq”=>“1”,
“time_sec”=>“1”}, “2”=>{“time_min”=>“2”, “title”=>“Qu\351 Onda Guero”,
“id_genre”=>“1”, “id_record”=>“6”, “id_artist”=>“7”, “isactive”=>“1”,
“seq”=>“2”, “time_sec”=>“2”}}}
Song Columns (0.000480) SHOW FIELDS FROM songs
SQL (0.000141) BEGIN
Song Load (0.000364) SELECT * FROM songs WHERE (seq IS NULL AND
id_record IS NULL) LIMIT 1
SQL (0.000145) COMMIT
SQL (0.000129) BEGIN
Song Load (0.000350) SELECT * FROM songs WHERE (seq IS NULL AND
id_record IS NULL) LIMIT 1
SQL (0.000152) COMMIT
SQL (0.000126) BEGIN
Song Load (0.000345) SELECT * FROM songs WHERE (seq IS NULL AND
id_record IS NULL) LIMIT 1
SQL (0.000135) COMMIT
SQL (0.000127) BEGIN
Song Load (0.000349) SELECT * FROM songs WHERE (seq IS NULL AND
id_record IS NULL) LIMIT 1
SQL (0.000122) COMMIT
SQL (0.000124) BEGIN
Song Load (0.000368) SELECT * FROM songs WHERE (seq IS NULL AND
id_record IS NULL) LIMIT 1
SQL (0.000125) COMMIT
SQL (0.000125) BEGIN
Song Load (0.000344) SELECT * FROM songs WHERE (seq IS NULL AND
id_record IS NULL) LIMIT 1
SQL (0.000124) COMMIT
Rendering within layouts/application
Rendering song/addautosave
Completed in 0.03609 (27 reqs/sec) | Rendering: 0.00294 (8%) | DB:
0.00417 (11%) | 200 OK

…which, although somewhat inscrutable, seems to indicate that the
params are getting passed correctly, but perhaps the db inserts are not
occurring as i had hoped.

also: when i try for some elegance to use a song hash in lieu of
spelling out object attributes…

@a_song = Song.new ( song )
@a_song.save

…i get the following equally inscrutable error message:

NoMethodError (undefined method `stringify_keys!' for 

“action”:String):

any help appreciated.

cheers,

-ian


#2

i should point out that my controller method is actually “addautosave”
… not “addsave” (as previously pasted)


#3

a bit of progress to report. if i hand-craft a single object…

def addautosave

    @song = Song.new (
        :isactive => 1 ,
        :id_artist => 7 ,
        :id_genre => 1 ,
        :id_record => 6 ,
        :seq => 1 ,
        :title => "bleh" ,
        :time_min => 2 ,
        :time_sec => 34
    )

    if @song.save
        redirect_to :action => "addauto"
    else
        render :action => "add"
    end

…all works well, so at least i can rule out my entire env being broke
:wink: