3

I currently have a database of compounds. For each compound, I have a series of data points, each of which consist of a time (t0, t2,t4,...,t24) and a corresponding value. I would like to extract the keys (t0, t2,t4,...,t24) and values and put them into separate arrays so I can graph them. I obtain my active record with the line:

data = Info.where('compoundName = ?',params[:cmpName])

Then I attempted to extract the keys and values using the lines:

a1 = data.keys
a2 = data.values

but I got the error:

undefined method `keys' for ActiveRecord::Relation::ActiveRecord_Relation_Info:0x007fafcdadba18

I looked this up and saw that the issue was that the sql query returned a data type that could not support these methods. But I am unsure how unpack the active record into the desired arrays. If I print out the active record it looks like this:

[#<Info num: 6, compoundName: "cmp1", t0: 78.77867, t2: 69.57333, t4: 68.95822, t6: 66.21941, t8: 65.37794, t10: 62.696, t12: 60.85907, t14: 60.40803, t16: 58.97237, t18: 59.55294, t20: 57.79256, t22: 57.17229, t24: 56.31774>]

Does anyone know how to accomplish this? Thanks!

5 Answers 5

1

If you just want to convert the sql object into array, you can simply do this:

data = Info.where('compoundName = ?',params[:cmpName]).to_a

and it will perform the fetch.

Sign up to request clarification or add additional context in comments.

Comments

1

All you have to do is this,

data_hash = Info.where('compoundName = ?',params[:cmpName]).first.as_json.select{|v| v=~ /^t[02468]*/}

a1 = data_hash.keys
a2 = data_hash.values

As suggested by @suslov ...

Comments

0

First, I'd rename your "Info" class to "Compound" to make it apparent what you're working with :-)

That said, Info.where() is returning all records that match, so if there's only one (or you only care about one) then use Info.where().first.

If you want to refer to your tN properties as "keys" and "values" then define those methods in your model.

2 Comments

My database is guaranteed to only have one tuple with the compoundName so I think Info.where() will return just the one record that matches
It will only find one record that matches, but it will return it in an array. This is a single object: {}this is a single object in an array: [{}]. Use .first as Hassan says to get the former.
0

Use as_json:

Info.where(compoundName: params[:cmpName]).as_json

So you can use .keys and .values methods.

2 Comments

I am trying to make the elements: num, compoundName, t0,t2,t4,....,t24 into keys. Then I am trying to make the numbers after the colon in the active record the values. For example, if the active record contains t0:78.77, then t0 is the key and 78.77 is the value
@adub: Updated.
0

So the query is currently returning all records which match on compoundName. If you only want one, you should make the query: data = Info.where('compoundName = ?',params[:cmpName]).first

I assume keys and values are methods on the Info model? Maybe something like models/info.rb:

class Info < ActiveRecord::Base
  def keys
    (0..24).map { |n| "t#{n}" }
  end

  def values
    keys.map { |key| self[key] }
  end
end

And make sure you're calling those methods on individual Info records.

You should look into storing the data in array form in the database though.

1 Comment

Sorry I am new to ruby on rails, would I define these methods in info.rb? I tried doing that but when I try to use the methods it still says that keys and values are undefined methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.