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
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.
@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
}
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.
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.