Rails how to query a hash in a where clause - ruby-on-rails

Is there a way with active record sql server adapter to query a hash field? I have a field called properties that is stored like this:
--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
summary: There is a problem
optional_information: Help!!
location: '28288'
reported_phone: "(555) 555-0352"
reported_email: person#email.com
affected_person_id: '1234'
affected_person_name: 'Tester'
affected_person_phone: '(555) 555-0352'
affected_person_email: person#email.com
I want to set up a query so that I pass a param through called filter and query either the summary field of the hash. Is this possible?
Trying to do somethings like this:
#requests = current_user.requests.where("properties->>'summary' = ?", '%#{params[:filter]}%') if params[:filter].present?
UPDATE: Per the advice below I changed the model to serialize the field as JSON. I also ran a migration to change the DB field type to JSON. Here is what the field looks like now. Still trying to figure out how to access and query the individual values.
{"summary":"Need bed raised","optional_information":"too low","location":"26679","reported_phone":"(555) 334-1324","reported_email":"mail#mail.com","affected_person_id":"1231232","affected_person_name":"testuserJ ","affected_person_phone":"(555) 334-1324","affected_person_email":"mail#mail.com"}

Related

Remove group_by field from final result rails

I am using rails 5.2.0.
I have a City model in which id is a Primary key and name represents name of city.
I am using group_by to achieve something like this:
Expected Output:
{10571=>"Μorocco Town", 17741=> "S-HERTOGENBOSCH"}
I am trying something like this:
City.all.select('name', 'id').group_by(&:id)
The result I am getting is:
10571=>[#<City id: 10571, name: "Μorocco Town">], 17741=>[#<City id: 17741, name: "S-HERTOGENBOSCH">]}
The key part is correct.
I need to get rid of the id field coming in the hash value and also, a single hash value instead of an array with name as key.
Any suggestions?
Using group_by is not mandatory. Any other inbuilt functions will also work.
Pluck is your friend...
City.pluck(:id, :name).to_h

Rails 4 update_all and set value from another field

I need to do some bulk updates in some models and set value of a field as value of another field.
Right now I can do that with raw sql like this:
ActiveRecord::Base.connection.execute("UPDATE `deleted_contents` SET `deleted_contents`.`original_id` = `deleted_contents`.`id` WHERE `deleted_contents`.`original_id` is NULL")
This is working fine, however I need to do this using ActiveRecord query interface due to many reasons.
I tried:
DeletedContent.where(original_id: nil).update_all(original_id: value_of_id_column)
For value_of_id_column I tried :id, self.id, id, etc, nothing works. What should I set for value_of_id_column to get the original query generated by rails? Is this possible, or using the raw sql is the only solution?
Also I do not want to iterate over each record and update. This is not a valid solution for me:
DeletedContent.where(original_id: nil).each do |deleted_content|
update_each_record
end
I'm pretty sure you cannot obtain that query by passing a hash to update_all.
The closest to what you want to obtain would be:
DeletedContent.where(original_id: nil).update_all("original_id = id")

How does one access the current id sequence value in the activerecord initializer?

I'm building an activerecord to model a conversation tree, using an array column type to represent the materialized path of the record's place in that tree, using postgres 9.1, rails 4.0, and the pg gem.
What I really want to do is access currval('conversations_id_seq') when I create a new conversation object, so that I can pass in [grandparent_id, parent_id ... current_id] as the array to the object initializer. That way I can specify that this column is not null as a database constraint, and in the event of a parentless conversation, have it still default to [current_id].
The problem I have is getting access to the model's id value before I save it the first time. I could always relax the not null constraint and add an after_create hook, but that feels kludgy. I'm hopeful that there's a way I can grab the value that's getting pushed into #id inside the initializer, before the first save to the database.
EDIT to clarify for the bounty: In an ideal world, there would be a special token I could pass in to the object's create method: Conversation.create(reply_chain: [:lastval]), where the gem took that to mean lastval() in the generated SQL.
something like:
def before_create
self.id=Conversation.connection.execute("SELECT nextval('conversations_id_seq')")
self.path = [... , self.id];
true
end
or use a before insert/update trigger to maintain the path.
You could alias the attribute if you don't need the column in the database.
alias_attribute :current_id, :id
Or you could query for the id when you need it.
def self.last_val
ActiveRecord::Base.connection.execute("SELECT lastval('conversations_id_seq')")
end
def self.next_val
ActiveRecord::Base.connection.execute("SELECT nextval('conversations_id_seq')")
end
Conversation.create(reply_chain: Conversation.next_val)
Using after_save isn't the ugliest of code either.

mongoid update_attributes changing datatypes

Im creating a simple rails app to modify data in an existing mongo database. I'm using mongoid for the interaction and can read/destroy objects just fine.
The problem comes is my mongo document has a 'node' which is a bunch of key value pairs with vary depending on the record. When i load the record like so:
MongoObject.find(BSON::ObjectId('ABC1234567890'))
=> #<MongoObject _id: ABC1234567890, node: {"totallogins"=>11, "id"=>"logIns"}>
I'm using a standard rails form to update the values so the post data looks like:
{"commit"=>"Edit", "utf8"=>"✓", "id"=>"ABC1234567890", "mongo_object"=>{"node"=>{"totallogins"=>"12", "id"=>"logIns"}}
If i then do:
#mongo_object.update_attributes(params[:mongo_object])
This works but changes the datatype of "totallogins" from an int to a string because the post data is a string.
Now active record deals with this itself but i need a solution that will work with mongoid.
Any ideas how i can do this?
Thanks. Unfortunately i can't as the fields for node are totally dynamic so i can't define them. I've come up with the following solution but its a tad ugly:
#mongo_object.node.each do |k,v|
new_value = params[:mongo_object][:node][k.to_sym]
new_value = new_value.to_i if v.class == Fixnum
#mongo_object.node[k] = new_value
end
#mongo_object.save
If you make the node an embedded_document, then you can explicitly set the field types when you declare them.
class Node
include Mongoid::Document
embedded_in :mongo_object
field :totallogins, type: Integer
...
end
http://mongoid.org/docs/documents/ mentions how to deal with types; perhaps make sure your type is an Integer?

How to get table column value?

I write follow code to get one record from the table webeehs:
webeehs_result = Webeeh.find(:all, :conditions=>["webeeh_project_id=#{project_id}"])
Then I want to get one column value from this record, how could I do?
For example, the column name is webeeh_date.
first of all, never EVER write code like that. Building your own conditions as pure strings can leave you vulnerable to SQL injection exploits. If you must do conditions, then do it like this:
:conditions => ["webeeh_project_id = ?", project_id]
if you have a Project model, you should rename the webeeh_project_id column from your Webeeh model into project_id and have an association in your Project model like has_many :webeehs
Then, you won't need to call that find anymore, just do a p = Project.find(id) and then p.webeehs will return the webeehs you need.
the result will be an array which you can iterate through. And to get your webeeh.webeeh_date member, just call it like this:
result.each do |webeeh|
date = webeeh.webeeh_date
end
webeehs_result = Webeeh.findwebeeh_dates
is enough to get all columnn values.
For a different method and performance issues check the following: http://www.stopdropandrew.com/2010/01/28/finding-ids-fast-with-active-record.html
webeeh_result will usually be an array of results for the database.
You can iterate throughit using
webeehs_result.each do |webeeh|
# use "webeeh.webeeh_date" to access the column_name or do whatever you want with it.
end

Resources