Did you ever faced the situation where you need to store the object in the table column? Did you ever faced the situation where you need to add the attribute dynamically to the table?
Rails allows us to store the object in the table column using 'serialize'. This is somewhat tricky to explain the use of storing the object in the database. You can easily understand when you face the scenario. Let's think out a simple example:
create_table :users do t
t.string :login
t.string :email
t.string :address
end
Note that address is a string field.
class user < ActiveRecord::Base
serialize :address
end
We can save the address as follows:
address = Addres.new(params[:address])
user = User.first
user.address = address
user.save
You can also store hash in the table column like below:
user = User.first
user.address[:street]= "Mettu Street"
user.address[:zip]= 23456
user.save
Use this concept when we want to add attribute to the table dynamically
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment