Problem with complex function to save in db

hello there, i have a problem.
i have my own complicated way that i want(must) save in the db
i have a form for some table (“factions” in my cans, each field in the
form goes to a single row of the “faction_contents” table, and then
the id of that row goes to the table “factions” in the row of which
the form field is for.

its a complicated thing and ugly, i know, but i don’t want suggestions
to not do it, i simply want you to see what i did wrong with my
function and help me fix it.

this is my function:
def save_id(arguments, tables)
arguments_id = arguments
arguments.each_pair do |key, value|
pair = { :english => value}
tables[“content”].new.save(pair)
content_id = tables[“content”].find(:first, :order => “id
DESC”).id
# @pair2 = {key => @text_id}
arguments_id[key] = content_id
end
tables[“table”].new.save(arguments_id)
end

the arguments are the parameters i get from the form
the tables variable holds the models i use and looks like this:
TABLES = {“table” => Faction, “content” => FactionContent}

the problem is when I do call this function it doesn’t put any content
in the db, it makes rows that their fields are NULL.

now people. i once again tell because it’s important. I know it’s and
I know there must be a simpler and easier way, but please just help me
fix this function and don’t suggest other ways. TY

TEREN wrote:

this is my function:
def save_id(arguments, tables)
arguments_id = arguments
arguments.each_pair do |key, value|
pair = { :english => value}
tables[“content”].new.save(pair)
content_id = tables[“content”].find(:first, :order => “id
DESC”).id
# @pair2 = {key => @text_id}
arguments_id[key] = content_id
end
tables[“table”].new.save(arguments_id)
end

the arguments are the parameters i get from the form
the tables variable holds the models i use and looks like this:
TABLES = {“table” => Faction, “content” => FactionContent}

Well… can you show us what an actual POST looks like from your log?
It would help to see the contents of the actual params hash.

sorry but I’m kinda new with rails, i don’t know how to get a log of the
hash itself.
how do I do that?

Almog Friedman wrote:

sorry but I’m kinda new with rails, i don’t know how to get a log of the
hash itself.
how do I do that?

development.log should be in a folder like:

C:\rails\appname\log\development.log

or

home\druid\rails\appname\log\development.log in a *nix envt.

heres the log hope it’ll help:

Processing FactionsController#index (for 127.0.0.1 at 2010-09-23
16:03:25)
[GET]
SQL (0.1ms) SET NAMES ‘utf8’
SQL (0.1ms) SET SQL_AUTO_IS_NULL=0
Faction Load (0.4ms) SELECT * FROM factions
Rendering template within layouts/factions
Rendering factions/index
Faction Columns (0.8ms) SHOW FIELDS FROM factions
Completed in 89ms (View: 83, DB: 1) | 200 OK [http://localhost/factions]

Processing FactionsController#new (for 127.0.0.1 at 2010-09-23 21:46:35)
[GET]
SQL (0.1ms) SET NAMES ‘utf8’
SQL (0.1ms) SET SQL_AUTO_IS_NULL=0
Faction Columns (0.8ms) SHOW FIELDS FROM factions
Rendering template within layouts/factions
Rendering factions/new
Completed in 43ms (View: 38, DB: 1) | 200 OK
[http://localhost/factions/new]

Processing FactionsController#create (for 127.0.0.1 at 2010-09-23
21:46:39)
[POST]
Parameters: {“commit”=>“Create”,
“authenticity_token”=>“1f1aa67b947f0e23dc19aad8debc1e63b6df002c”,
“faction”=>{“name”=>“asdasd”, “name_plural”=>“asdasd”,
“description”=>“asdasdasd”}}
SQL (0.1ms) SET NAMES ‘utf8’
SQL (0.1ms) SET SQL_AUTO_IS_NULL=0
Faction Columns (0.9ms) SHOW FIELDS FROM factions
FactionContent Columns (0.7ms) SHOW FIELDS FROM faction_contents
SQL (0.1ms) BEGIN
FactionContent Create (7.4ms) INSERT INTO faction_contents
(created_at, updated_at, english) VALUES(‘2010-09-23 19:46:39’,
‘2010-09-23 19:46:39’, NULL)
SQL (29.0ms) COMMIT
FactionContent Load (0.3ms) SELECT * FROM faction_contents ORDER
BY id
DESC LIMIT 1
SQL (0.0ms) BEGIN
FactionContent Create (0.2ms) INSERT INTO faction_contents
(created_at, updated_at, english) VALUES(‘2010-09-23 19:46:39’,
‘2010-09-23 19:46:39’, NULL)
SQL (31.1ms) COMMIT
FactionContent Load (0.2ms) SELECT * FROM faction_contents ORDER
BY id
DESC LIMIT 1
SQL (0.0ms) BEGIN
FactionContent Create (0.1ms) INSERT INTO faction_contents
(created_at, updated_at, english) VALUES(‘2010-09-23 19:46:39’,
‘2010-09-23 19:46:39’, NULL)
SQL (31.3ms) COMMIT
FactionContent Load (0.2ms) SELECT * FROM faction_contents ORDER
BY id
DESC LIMIT 1
SQL (0.1ms) BEGIN
Faction Create (0.2ms) INSERT INTO factions (name, created_at,
updated_at, name_plural, description) VALUES(NULL, ‘2010-09-23
19:46:39’, ‘2010-09-23 19:46:39’, NULL, NULL)
SQL (29.8ms) COMMIT
Redirected to actionindex
Completed in 147ms (DB: 132) | 302 Found [http://localhost/factions]

Processing FactionsController#index (for 127.0.0.1 at 2010-09-23
21:46:39)
[GET]
SQL (0.1ms) SET NAMES ‘utf8’
SQL (0.1ms) SET SQL_AUTO_IS_NULL=0
Faction Load (0.6ms) SELECT * FROM factions
Rendering template within layouts/factions
Rendering factions/index
Faction Columns (0.9ms) SHOW FIELDS FROM factions
Completed in 85ms (View: 80, DB: 2) | 200 OK [http://localhost/factions]

any help?

Hmm… maybe you do need a table name involved:

Controller:

def create
hash = params[:faction]
hashnew = save_ids(hash, ‘FactionContent’)
Faction.create(hashnew)
@faction = Faction.find(:last, :order => ‘id’)
end

And that other routine:

def save_ids(arguments, table)
ret = Hash.new
arguments.each_pair {|key, value|
table.constantize.create(:english => value)
content = table.constantize.find(:last, :order => ‘id’)
ret[key] = content.id.to_s
}
return ret
end

Almog Friedman wrote:

heres the log hope it’ll help:

Processing FactionsController#create (for 127.0.0.1 at 2010-09-23
21:46:39)
[POST]
Parameters: {“commit”=>“Create”,
“authenticity_token”=>“1f1aa67b947f0e23dc19aad8debc1e63b6df002c”,
“faction”=>{“name”=>“asdasd”, “name_plural”=>“asdasd”,
“description”=>“asdasdasd”}}

FactionContent Create (7.4ms) INSERT INTO faction_contents
(created_at, updated_at, english) VALUES(‘2010-09-23 19:46:39’,
‘2010-09-23 19:46:39’, NULL)

no values for ‘english’ in any of the inserts

I think you’re over-complicating things…

In factions_controller.rb:

def create

just grab the hash for the model being saved

hash = params[:faction]

do the string to content record id magic

hashnew = save_ids(hash)

save a new Faction record

Faction.create(hashnew)

find the one just saved

@faction = Faction.find(:last, :order => ‘id’)
end

And wherever you have this stored:

def save_ids(arguments)

saving the content is table agnostic, don’t need that parameter

new empty hash to return

ret = Hash.new

for the incoming arguments hash

arguments.each_pair { |key,value|
# store each value in Contents
Content.create(:english => value)
# find the record just written
content = Content.find(:last, :order => ‘id’)
# replace the string just stored with a stringified id from Contents
ret[key] = content.id.to_s
}

return our new hash

return ret
end

worked for me…

ok, i think this should work ty very much. i really appreciate it.
but I have one question, I didn’t quite understand why did you put
that: @faction = Faction.find(:last, :order => ‘id’) at the end of the
controller action.

Almog Friedman wrote:

I didn’t quite understand why did you put
that: @faction = Faction.find(:last, :order => ‘id’) at the end of the
controller action.

In the code I posted, there is no @faction object populated from the
params, and standard routing after a create is to go to the show for
that @faction (the usual respond_to do |format| block).

Faction.create doesn’t create one either, so I retrieved the one just
created.

yea mannered to figure it out eventually.
well again i wanna thank you, it worked(with slight modifications to fit
right) thank you so very much. I’ve been trying to make this function
work
for over a week and you solved it less than a day.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs