We can use Nosql in Postgres :-), Excited right ? Yep Ruby on Rails 4 supports this via Hstore.
I had to query my Model tuples but it was not possible as i was using Serialize to store dynamic fields. But using Hstore we can easily achieve this like as follows , Hstore acts same as serialize in rails, but additionally we can query tables using Hstore.
console
p = Chocolate.last
p.properties = {'color' => 'orange'}
p.properties['price'] = '5'
p.save
Chocolate.where("color -> 'orange' ")
Chocolate.where("price -> '5' ")
Follow steps below to enable Hstore in Rails 4 + Postgresql applications.
rails generate migration enable_hstore_extension
class EnableHstoreExtension < ActiveRecord::Migration
def change
enable_extension 'hstore'
end
end
rake db:migrate
Now create a migration that will add a column called 'properties' to our Chocolate model.
rails g migration AddPropertiesToChocolate
class AddPropertiesToChocolate < ActiveRecord::Migration
def change
add_column :chocolates, :properties, :hstore
end
end
rake db:migrate
Good one :)
ReplyDeleteThank u Tevin :)
DeleteIt's a very useful article. It can help a lot to the viewers to enhance their ror development skills. Ruby on Rails Developers
ReplyDeleteHi,
ReplyDeleteNice article for ror folks. But try with Array type in postgresql.