Mongoid with Rails 3
Hi, Every one , I feel that my posts are useful for you to get knowledge on Rails and please suggest me if you have any useful topics so that we will discuss more on those topics . here I am going to explain how Mongoid works with rails3 , here I explained some basics to get start with mongoid .
as i have posted previously mongodb is so flexible and its document oreiented . We have sevaral ORM 's in the market we all know that ActiveRecord is one of the famous ORM for rails when we are dealing with relational databases . Now here we are dealing with ODM 's means Object Document Mapper . There are several ODM ' s are there few things i listed out here in conjuction with rails
1.MongoMapper
2.Mongoid
3.Mongomatic
4.MongoODM
5.MongoModel
6.DriverAPILayer
these are all ruby based ODM's Here i am going to create small application which will give some idea about how to use Mongoid
Step 1:
install the mongoid gem by typing the following at your terminal
"gem install mongoid " or else you can add it your gem file
Step2 : create a new rails application
rails new mongoid_app
Step3 : go to your mongoid_app
cd monoid_app
Step 4 : here I want to create two models Movie and Dancer and later i will associate both .
First generate scaffold for movie model
rails g scaffold Movie name:string
which will create model , controller and veiws for movie model , in the same way create dancer model also
rails g Dancer name:string
then open your model and simply just include the following line in your model
include Mongoid::Document
follow the example below Ex :
class Movie
include Mongoid::Document
field :name , type:String
end
this will give ability to our class to act as a mongo document . Here we can see that our movie model contains only one field called name , if we want to add extra fields then no need of migrations here ,(oh sounds cool) simply add field name and type , once you enter the data and run the application it will add to database really how simple it is ! I will add one more field here
class Movie
include Mongoid::Document
field :name , type:String
field :budget , type : Integer
end
in the same way include Mongoid::Document to the Dancer model also .
class Dancer
include Mongoid::Document
field :name , type:String
end
now i will associate both models with has_many relation , just like the activerrecord relations we can add here also here I am going to establish Movie has_many dacers . See the following code snippet >
class Movie
include Mongoid::Document
field :name , type:String
field :budget , type : Integer
embeds_many :dancers
end
and Dancer model is class Dancer
include Mongoid::Document
field :name , type:String
embedded_in :artist
end
now we can query model simply by the following
fetch Movie Model :
@movie = Movie.first
fetch movie related dancers
@dancers = @movie.dancers
I hope this will help you to give some idea mongoid with rails .I welcome your comments on this article so that i can improve further .thank you Thanks Ratnakar V
0 comments: