Ruby on Rails: Saving query string parameter value to database - ruby-on-rails

I search google for this question but i did not find any solution for
that.
I want to save parameter value to database. this parameter send by url.
for example:
http://simple-beach-416.heroku.com/code/restserver?fname=cv
I want to save fname value(here "cv") to database.
how can i do that?

Make a model for it? This is very vague... but define a model called Fname and give it the attribute "value"
Then...
Fname.create(:value => params[:fname])
I think this is where you're going, yeah?

Related

Ruby on Rails - using a block parameter as a method call

I'm having trouble with a little Ruby on Rails I'm building and need some help.
I have a Table with 20+ Columns and a corresponding XML File which can be parsed as some sort of hash with a gem. Every key would be mapped to a column and every value would be a data record in said column.
The way I access a specific value in the already parsed XML file is:
filename["crs","inputkeyhere"]
which returns the value, for example "52" or whatever.
What I am trying to do is upload the file, parse it with the gem and give each column the corresponding value.
My table (or model) is called "Attributeset" and I already know how I can access every column:
#attributeset = Attributeset.new
#attributeset.attributes.keys
So my thought process was:
Iterate over all the keys
Pass every key into a block called |a|
Use the rails possibilty to set attributes by calling the corresponding #attributeset.
Set colum attribute to the corresponding xml key
So my code would go something like this:
#attributeset.attributes.keys.each do |a|
#attributeset.a=filename["crs",a]
end
But my problem is, that ruby thinks ".a" is a method and apparently does not evaluate "a" to the block parameter.
I've read through lambdas and procs and whatnot but didn't really understand how they could work for my specific situation.
Coming from bash scripting maybe my thinking might be wrong but I thought that the .a might get evaluated.
I know I can run the block with yield, but this only works in methods as far as I know..
Any help is appreciated.
Thanks and stay healthy,
Alex
Thanks for the input!
I wanted to make it as clean as possible, and not using any temporary hashes to pass arguments.
I've found the method
write_attribute
which can be used like this:
#attributeset.write_attribute(a, xmp["crs",a])
worked perfectly for me.
You can use []= method to set values dynamically:
#attributeset.attribute_names.each do |attribute|
#attributeset[attribute] = filename["crs", attribute]
end

Returning a hash instead of array with ActiveRecord::Base.connection

I have to use a query like this :
query = Enc.joins(:rec).group("enc.bottle").
select("enc.bottle as mode, count(rec.id) as numrec, sum(enc.value) as sumvalue")
That I use with :
#enc = ActiveRecord::Base.connection.select_all(query)
To get the data, I've to do #enc.rows.first[0] (it works)
But #enc.rows.first["mode"] doesn't work ! Because each row of #enc.rows contains array.. not a map with the name of each field.
Maybe select_all is a wrong method.
Does it exist another method to get the data with the name of field ?
Thank you
EDIT
If you can associate a model with the query, then there's no need for the generic select_all method. You can use find_by_sql like this:
Enc.find_by_sql(query).first.mode
# => testing
Note that you will no be able to see the aliases when inspecting the results, but they are there. Also, the convention is to use plural names for the tables. You might find it easier to just sticks with the defaults.

append a string to the params name

In my rails app I am stuck with a situation where I need to add a string to the params name,
i.e, I have a params array params[:attended], but I need to have this as params[:attended_61] and _61 must be appended. 61 is the ID of an active record row object. I have that value in #sys.id. Then please tell me how I can convert params[:attended] to params[:attended_61]. Thank you.
Billy Chan is right, what you're trying to do sounds very strange and you should probably rethink your whole approach.
But if you must do such an odd thing, you could just do this:
params[:"attended_#{#sys.id}"] = params.delete(:attended)
Or, since params will be a HashWithIndifferentAccess, you could skip the symbolification:
params["attended_#{#sys.id}"] = params.delete(:attended)
# or even
params["attended_#{#sys.id}"] = params.delete('attended')
params is simply a method that returns a Hash, you can change that Hash as needed.
You can do that as given below.
params["attended_#{#sys.id}"]
Perfectly working !!
also you can add : to the params name to be like the natural params[:var] name and it will work well also to be like that
params[:"attended_#{#sys.id}"]

Getting Mongoid from params array

In order to find a Root Document that contains a embedded document using MongoID/Rails 3 I need to do my query this way:
QuoteRequest.where( "order_request_items._id" => BSON::ObjectID(params[:id]) ).first
Is there a way to query without using the BSON::ObjectID ?
Thanks!
I'm not a MongoID/Rails user, but my guess is that you can't.
Even in the Mongo shell you have to use ObjectId() if you want to compare ObjectIDs. Something like this won't return any results:
db.foo.find({_id: "4c7ca651db48000000002277"})
You'll have to create an actual ObjectID from the string in order to get results:
db.foo.find({_id: ObjectId("4c7ca651db48000000002277")})
MongoID apparently doesn't automatically convert your input to ObjectIDs. But perhaps there's a way to tell MongoID which fields it should always convert to ObjectIDs? Then you would be able to omit the use of BSON::ObjectID.
This is a bug, the ids should be automagically converted by Mongoid. You should open a ticket on github: http://github.com/mongoid/mongoid/issues

how to parse multivalued field from URL query in Rails

I have a URL of form http://www.example.com?foo=one&foo=two
I want to get an array of values ['one', 'two'] for foo, but params[:foo] only returns the first value.
I know that if I used foo[] instead of foo in the URL, then params[:foo] would give me the desired array.
However, I want to avoid changing the structure of the URL if possible, since its form is provided as a spec to a client application. is there a good way to get all the values without changing the parameter name?
You can use the default Ruby CGI module to parse the query string in a Rails controller like so:
params = CGI.parse(request.query_string)
This will give you what you want, but note that you won't get any of Rails other extensions to query string parsing, such as using HashWithIndifferentAccess, so you will have to us String rather than Symbol keys.
Also, I don't believe you can set params like that with a single line and overwrite the default rails params contents. Depending on how widespread you want this change, you may need to monkey patch or hack the internals a little bit. However the expeditious thing if you wanted a global change would be to put this in a before filter in application.rb and use a new instance var like #raw_params
I like the CGI.parse(request.query_string) solution mentioned in another answer. You could do this to merge the custom parsed query string into params:
params.merge!(CGI.parse(request.query_string).symbolize_keys)

Resources