Welcome to Working With Rails

 

Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
how to assign each value to its corresponding keys in generic ways
8 Posts
how to assign each value to its corresponding keys in generic ways

Hi all - my problem is that i get key and value .but i have to save in the database. but while saving it will save only one key and corresponding value ,but if i want to save more than one value to corresponding keys ,it shows problems. but any how i got all the keys and values .only my problem is how i assignee each key to value.it should be in generic way.i have search it but i do not get the right way.

                             my code is like this:-

bold local_keys = @params.keys, first_addition = true, param_value =0, sql=String.new, sql_value=String.new, for key in 0...local_keys.length list=key.methods,

    key_name = local_keys[key],
    value =  @params[key_name],
     puts "key "+key_name.to_s+ " value "+value.to_s, 
      if key_name!="action" && key_name!="controller" ,
      @test_runs = TestRuns.new,
         @test_run.ks=value(it is successful)(here ks is my column name),

but when i want to update more than one in a generic way it is not successfull like

@test_run.key_name=value(it is not working) *bold*

Hi Bishnu

I am not very clear why a loop is used , if all you want is to catch the params posted by a form and store them in the database, you can just do this in your controller

   @test_runs = TestRuns.new
   @test_runs.attributes = params[:XXXXXXX]
  @test_run.save

Correct me if your requirement is something else.

Person
Forum Rating: 100
Sign In To Rate Post

Hi Balaji

            Thanks for Your suggestion.     my  requirement is that ,user can send  2/3/4/5 no of parameters values . my table has 20-25 columns.every time user sends value will be save in database with new id.it depends  upon user what is no of value he send. that's why i use key value pair.so can it be done through ruby reflection.what you suggest i tried for one parameter ,error is comming like this:-"You have a nil object when you didn't expect it!You might have expected an instance of ActiveRecord::Base.The error occured while evaluating nill.attributes=".can you suggest me why the error is ? 2:-how can i do it in a generic way like user may send any 2/3/4/5 values. 3:- how can i do for more than 1 value what you suggested before.

Can you please post the modified code that you are using now.

Person
Forum Rating: 100
Sign In To Rate Post

http://127.0.0.1:3000/test_runs/save?duration=2300000&sut=qa1

local_keys = @params.keys puts local_keys first_addition = true param_value =0 sql=String.new sql_value=String.new for key in 0...local_keys.length list=key.methods

    key_name = local_keys[key]
    value =  @params[key_name]
    puts "key "+key_name.to_s+ " value "+value.to_s 
      if key_name!="action" && key_name!="controller" 
      @test_runs = TestRuns.new

    @test_runs.duration=value
    @test_runs.sut=value

 it is working fine

i need generic way like

@test_runs.key_name=value

in this key_name contains attributes duration,sut what ever i have sent though url.

But my problem is i could not able to insert using key_name

Why it is important means later may be coulmns added in the db i can use this code with out change code that it is main intention

i tried using reflection like this

if key_name!="action" && key_name!="controller"

        @test_runs = TestRuns.new

        if @test_runs.respond_to?(key_name)

        puts "has a method VVVVVV"+key_name

          method_obj = method(key_name)

          method_obj.call(value)

          puts "passing value VVVVVV"+value

        end

This is not working.

Please help me in this regard

Then try something like this example

a= {"a"=>"first","b"=>"second","c"=>"third"}

a.each do |key,value|

puts "#{key} -- #{value}"

end

also @params has been deprecated so simply use params

In case of any clarifications please mail me at balaji.ananthachari@gmail.com

Person
Forum Rating: 100
Sign In To Rate Post

thanks it is working.

final solution is

@test_runs[:"#{key_name}"] = value. this will assign each value to the corresponding keys in generic ways. Again thanks mr Balaji for Your solution.

8 Posts
Login to add your message