Our main domain object has multiple string[] properties (various configuration options) and we are thinking of an elegant way to persist the data. GORM creates joined table for the each array so we end up with about dozen joined tables.
I wonder if it would be possible to serialize each array into single column of the main table (someway delimited) and parse it back into array onload?
Do you have suggestions how to do this? I'm thinking either hibernate usertype or grails property editor? I spent some time with usertypes but without luck.
thanks
pk
you could put the parameters into a map / array, then store them in a db field as Json:
def someDomainInstance = new SomeDomain()
def paramMap = [name:'John', age:24]
someDomainInstance.paramJson = paramMap as JSON
someDomainInstance.save()
Then you can easily convert this string back to a map / array when you interrogate the DB:
def paramMapFromDB = JSON.parse(someDomainInstance.paramJson)
assertEquals 24, paramMapFromDB.age
Something like that, I haven't tested the syntax, but that's the general idea.
Related
I need all the documents in one of my collections to create association in between my parent model to child. The problem is I only have the string of my ObjectId. So I am finding the object by string and then set via parent.child = foundObject. So, to achieve this I created a private method as below, to not to create DB request each time I need that child object.
def childs
#childs ||= Child.all
end
But this is not working as expected. When I run ModelName.all it returns below result; not all the docs in collection.
=>
#<Mongoid::Criteria
selector: {}
options: {}
class: ModelName
embedded: false>
And this causes my loop to create another DB request each time I try to associate child to parent. I prevent this by using below method.
def childs
#childs ||= Child.all.select { |v| v.id.present? }
end
I believe there should be a way of collecting all documents in MongoDB, I know the idea of Mongoid::Criteria and what it actually does. But in some case, I need all the objects to be stored in one variable. Do not want to create unwanted DB queries each time I need one specific document in a model.
I could not find a way to solve this specific problem and I think it's kind of impossible since MongoDB is not a relational DB It's quite hard to collect information at the same time with querying. What I used is "MongoDB views" and this solved a lot. Here is the docs. There you can read and find yourself an approach to figure out your own problem.
I have two tables in my rails app: properties and requests. Both tables have field called address. But in property address is always single value like Hollywood for example and in request table there could be complicated string like ["Hollywood", "Beverley Hills"]. My task is to get all properties which match by address. It means that if we have in request ["Hollywood", "Beverley Hills"] i need all properties that have address as Hollywood and all Beverley Hills. I tried something like this:
#properties = Property.where("address = ? ", #request.address)
and:
#properties = Property.where("address IN (?) ", #request.address)
but both variants don't work and i think because #request.address is actually string, not array.
So i would like if somebody would suggest me some good solution.
Your first try is wrong, as address in Property is a single value.
Your second try is correct but not the best.
You can use Property.where(address: #request.address). But you have to be sure that #request.address is an Array of String.
You shouldn't save an Array as a String like that: "[\"Hollywood\", \"Beverley Hills\"]. It is too hard to parse in the application. If you want to save this way, you will be better using serialize :address, Array in the model, because then it will return an Array when you try to access the attribute.
Anyway, check if #request.address in an Array of String, if not, parse it to be an Array of String.
You can just wrap it in an array
#properties = Property.where(address: [#request.address])
So I am working on this existing rails application where I am accessing 2 tables from 2 different databases.
scope :comp_ids_in, lambda {|comp_ids| where(:comp_id => comp_ids)}
company_info = CompanyInfo.comp_ids_in(my_array_of_ids)
the above company_info returns back an an array of ActiveRecord:Relation CompanyInfo objects.
Now I want to compare the above company_info objects with another table on a different database
and return back all the found results in an object.
My existing attempt in my controller would return back only 1 result at a time.
company_info.each do |info|
# RemoteInfo is an acive record class which accesses record from a different database
remote_info = RemoteInfo.where(username: current_user.username, property_code: info.org_id, chain_code: info.site_id)
end
I want all the results stored in the remote_info object. So that I can loop through that object and get any information that was returned.
I would appreciate it if some one can suggest me an efficient approach.
If you are not worried about the order, it would be efficient to just make one query to get all the remote info
remote_info = RemoteInfo.where(username: current_user.username, property_code: company_info.collect(&:org_id), chain_code: company_info.collect(&:site_id))
I have a controller that returns a list of categories and a count of items in each category
Category.joins(:item).group([:category_id,:name]).count
I would like to clean up the json formatting from this
(which is category_id, :name, :count )
{"[10, \"Fruit.\"]":2,"[11, \"Vegetables.\"]":1,"[2, \"Pasty.\"]":1}
to this
{"categoryitemcount":[ {"id":10,"name":"Fruit","count":7}]}
so a custom root name along with named columns
thanks in advance!
The solution to this is demoed in Railscast #219. Basically create a model not backed by a database and which represents the data you want to send back. In my case CategoryItemCount.rb (id, name, count). I built an array of these on the fly using the query given above, and output as json.
Question: Is it possible to build a class method scope that can query objects based on values inside an array in a table? If yes, how can I do this?
In my example, I have a “wells” table that has an array field called “well_tags”. I want to build a query that returns all objects that have a specified value (such as “ceramic”) in the wells_tags array. The basic query would be something like this:
#well = Well.all
#query = #well.where(“well_tags contains ceramic”)
And then the class method scope would look something like this, with the “well_tag_search” param passed in from the controller:
class Well < ActiveRecord::Base
def self.well_tag_filter(well_tag_search)
if well_tag_search.present?
where(“well_tags contains ceramic")
else
Well.all
end
end
I found another post that asks a similar question (see link below), but I cannot get the answer to work for me...the result is always 'nil' when I know there should be at least 1 object. I am a beginner using sqlite (for now) as my database and rails 4.0.
Active Record Query where value in array field
Thanks!
UPDATE: some progress
I figured out how to create an array of all the objects I want using the ‘select’ method. But I still need to return the results as an Active Record object so I create a class method scope.
#well = Well.select
{ |well| if well.well_tags.present?
then well.well_tags.include? ‘ceramic' end }
#well.class #=> array
Not sure where Show is coming from.
Can you try doing Well.all instead of Show.all?