Rails: Serialize value as comma-separated and not as YAML - ruby-on-rails

I'm looking for a way to store a serialized value of eg. IDs in a column. In before claims that this is not an optimal design: the column is used for IDs of associated records, but will only be used when displaying the record - so no queries are made with selection on the column and no joins will be made on this column either.
In Rails I can serialize the column by using:
class Activity
serialize :data
end
This encodes the column as YAML. For legacy sake and since I'm only storing one dimensional arrays containing only integers, I find it more suitable to store it as a comma-separated value.
I've successfully implemented basic accessors like this:
def data=(ids)
ids = ids.join(",") if ids.is_a?(Array)
write_attribute(:data, ids)
end
def data
(read_attribute(:data) || "").split(",")
end
This works pretty fine. However I'd like to add array-like methods to this attribute:
activity = Activity.first
activity.data << 42
...
How would I do this?

You can do it with composed_of feature as explained in this post.
It should be something like:
composed_of :data, :class_name => 'Array', :mapping => %w(data to_csv),
:constructor => Proc.new {|column| column.to_csv},
:converter => Proc.new {|column| column.to_csv}
after_validation do |u|
u.data = u.data if u.data.dirty? # Force to serialize
end
Haven't tested it though.

You can use serialize with a custom coder in rails 3.1.
See my answer to this question. :-)

Related

How to query from serialize Hash column in Rails

For example:
class User < ActiveRecord::Base
serialize :preferences, Hash
end
preferences contains hash record...
ex:
{'category' => 'a', 'type' => 'b', 'published' => true}
How can I do some query, where I have to return records that matched/equal to specific key-value in serialized column.
I want to have something like:
.where('preferences.published' => true)
Thanks in advance.
By default Rails serializes hashes as YAML, so you can do:
.where('preferences LIKE "%published: true%"')
You can't use native DB routines to query a serialized field.
The only way to do this query is to do
User.all.select { |u| u.preferences['published'] == true }
You only want to serialize fields when you don't want to query on it, and just expose the data 'as it'
If you need to global query on it, you should have a table UserPreference for example, with a user_id and the different preferences.
You can 'hack' it doing what #mudasobwa said .where('preferences LIKE "%published: true%"'), but it can be risky.

How can I save array of hashes into db?

I used nokogiri to parse a xml document into array of hashes:
helpers/countries.helper
module CountriesHelper
def parse
#countries = ['australia', 'canada', 'france']
#countries.inject([]) do |memo, country|
File.open("public/#{country}.xml") do |f|
xml = Nokogiri::XML(f)
path = "//country/stores/"
memo << xml.xpath(path).map do |x|
{ 'country' => x.parent['country'],
'store' => x['store']}
end
end
end
# [{"country"=>"australia", "store"=>"store1"}, {"country"=>"france", "store"=>"store2"}]
How can I save this array of hashes format into my database? Lets say I have two models Country and Store.
You can serialize an attribute, which means saving it as a particular type of object.
#in your model
serialize :store_hashes, Array
The field should be a text field in the database. I don't know whether or not that's a good idea in this particular instance - i suspect it isn't. But that's how you save an array of hashes to the database.
http://apidock.com/rails/ActiveRecord/Base/serialize/class
You can store your array of hashes in a text field in your database.
Something like this in your migration file:
create_table "your_table", force: true do |t|
t.text "your_column_name"
end
Or, if you already have the table in the database and just want to add the new column to the table:
class Migration0001
def change
add_column :your_table, :your_column_name, :text
end
end
Just so you know, if you want to save a Hash object in your database and if you define the column type as :text, then Rails should be able to serialize it properly, and you won't need to use serialize explicitly in your model.
But, in your case, it's an Array of Hash, so it's an Array object that needs to be saved in the database, so you need to serialize the field in your model:
serialize :your_column_name, Array
That way, you can save an Array of Hashes in the database. Hope this helps.
Assuming country has many stores. Storing the hash in database would make very little sense (in my opinion). Storing in individual tables would make much more sense and easy for querying.
module CountriesHelper
def parse
#countries = ['australia', 'canada', 'france']
#countries.inject([]) do |memo, country|
File.open("public/#{country}.xml") do |f|
xml = Nokogiri::XML(f)
path = "//country/stores/"
memo << xml.xpath(path).map do |x|
{ 'country' => x.parent['country'],
'store' => x['store']}
country = Country.find_by_name(x.parent['country'])
if country.nil?
country = Country.create(name: x.parent['country'])
end
country.stores.create(name: x['store'])
end
end
end
Database transactions are meant to be invoked from Model; you can refactor later.
class Country < ActiveRecord::Base
has_many :stores
end
class Store < ActiveRecord::Base
belongs_to :country
end

Querying embedded objects in Mongoid/rails 3 ("Lower than", Min operators and sorting)

I am using rails 3 with mongoid.
I have a collection of Stocks with an embedded collection of Prices :
class Stock
include Mongoid::Document
field :name, :type => String
field :code, :type => Integer
embeds_many :prices
class Price
include Mongoid::Document
field :date, :type => DateTime
field :value, :type => Float
embedded_in :stock, :inverse_of => :prices
I would like to get the stocks whose the minimum price since a given date is lower than a given price p, and then be able to sort the prices for each stock.
But it looks like Mongodb does not allow to do it.
Because this will not work:
#stocks = Stock.Where(:prices.value.lt => p)
Also, it seems that mongoDB can not sort embedded objects.
So, is there an alternative in order to accomplish this task ?
Maybe i should put everything in one collection so that i could easily run the following query:
#stocks = Stock.Where(:prices.lt => p)
But i really want to get results grouped by stock names after my query (distinct stocks with an array of ordered prices for example). I have heard about map/reduce with the group function but i am not sure how to use it correctly with Mongoid.
http://www.mongodb.org/display/DOCS/Aggregation
The equivalent in SQL would be something like this:
SELECT name, code, min(price) from Stock WHERE price<p GROUP BY name, code
Thanks for your help.
MongoDB / Mongoid do allow you to do this. Your example will work, the syntax is just incorrect.
#stocks = Stock.Where(:prices.value.lt => p) #does not work
#stocks = Stock.where('prices.value' => {'$lt' => p}) #this should work
And, it's still chainable so you can order by name as well:
#stocks = Stock.where('prices.value' => {'$lt' => p}).asc(:name)
Hope this helps.
I've had a similar problem... here's what I suggest:
scope :price_min, lambda { |price_min| price_min.nil? ? {} : where("price.value" => { '$lte' => price_min.to_f }) }
Place this scope in the parent model. This will enable you to make queries like:
Stock.price_min(1000).count
Note that my scope only works when you actually insert some data there. This is very handy if you're building complex queries with Mongoid.
Good luck!
Very best,
Ruy
MongoDB does allow querying of embedded documents, http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-ValueinanEmbeddedObject
What you're missing is a scope on the Price model, something like this:
scope :greater_than, lambda {|value| { :where => {:value.gt => value} } }
This will let you pass in any value you want and return a Mongoid collection of prices with the value greater than what you passed in. It'll be an unsorted collection, so you'll have to sort it in Ruby.
prices.sort {|a,b| a.value <=> b.value}.each {|price| puts price.value}
Mongoid does have a map_reduce method to which you pass two string variables containing the Javascript functions to execute map/reduce, and this would probably be the best way of doing what you need, but the code above will work for now.

Query on Mongoid Hash Field

I want to query on a Hash field for a Mongoid Class. I'm not sure how I can do this with conditions?
Here is an example:
class Person
include Mongoid::Document
field :things, :type => Hash
end
So, let's say that I have this:
p = Person.new
p.things = {}
p.things[:tv] = "Samsung"
I want to query for the first person with a tv that is a Samsung...
People.first(:conditions => ?????
Thanks in advance.
Person.where('things.tv' => 'Samsung').first
This is where Mongoid and MongoDB really shine. Mongoid's Criteria methods (Person.where, Person.any_of, Person.excludes, etc.) will give you much more flexibility than the ActiveRecord-style finders (passing a :conditions hash to Person.find, Person.first, etc.)
Mongoid's site has some great documentation on how to use Criteria:
http://mongoid.org/en/mongoid/docs/querying.html

How to coerce type of ActiveRecord attribute returned by :select phrase on joined table?

Having trouble with AR 2.3.5, e.g.:
users = User.all( :select => "u.id, c.user_id", :from => "users u, connections c",
:conditions => ... )
Returns, e.g.:
=> [#<User id: 1000>]
>> users.first.attributes
=> {"id"=>1000, "user_id"=>"1000"}
Note that AR returns the id of the model searched as numeric but the selected user_id of the joined model as a String, although both are int(11) in the database schema.
How could I better form this type of query to select columns of tables backing multiple models and retrieving their natural type rather than String ? Seems like AR is punting on this somewhere. How could I coerce the returned types at AR load time and not have to tack .to_i (etc.) onto every post-hoc access?
It's unfortunately not going to happen very easily. All of the data from the DB connection comes to rails as strings, the conversion of types happens in each of the dynamic attribute methods that rails creates at runtime. It knows which attributes to convert to which type by the table's column-type meta-data that it retrieves when the app starts. Each model only has column meta-data for it's own columns, that's why it's own columns end up with correct type. There is no easy way to auto-convert to the correct types.
You could on the other hand, create a simple conversion method that would take a Hash and automatically convert the attributes.
Something like this:
users = User.all(:select => "cl, comments.c2", ...)
users = convert_columns(users, 'c2' => :integer, 'other_column' => :date)
def convert_columns(records, columns = {})
records.each do |rec|
columns.each do |col, type|
rec[col] = case type
when :int then rec[col].to_i
when :date then ........
....
end
end
end
end
Why are you using :from => "users" inside a User.method ?
The following will do an inner join (which is what you are doing anyways)
users = User.all(:include => :connections, :select => "users.id, connections.user_id", :conditions => {...})
This is going to be very heavy query for the database.
Faster query would be with the outer join though.
This will also return the keys as INT not STRING
A much faster alternative was
Connection.all(:include => :user, :conditions => {...}).collect {|e| [e.user_id, e.id] }
This gives you an array of arrays with the ids. If you are going to select "id, user_id" columns only, then it may not necessarily be as AR object. An array can be faster.
I hope I am not missing some point here. Suggest me, if I am.
If you want quick solution - try to use after_find callback and preset correct attributes types there:
class User < ActiveRecord::Base
after_find :preset_types
private
def preset_types user
user.user_id = user.user_id.to_i
end
end

Resources