Ruby on Rails Development - How to Use Instagram with MongoDB
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Ruby on Rails development is one of the frameworks used by many developers for web and server side development across the world.

Ruby on Rails Development – Benefits

ROR development is always quick and fast. Developers can complete the whole project in 4 weeks which normally takes 12 weeks if done on other programming languages. Ruby on Rails development also makes your life easier while making changes in the website or adding features later on. This is only because ROR development offers;

– Leaner code-base
– Modular design
– Availability of existing plugins

Another amazing factor due to which Ruby on Rails developer use this language is its cost effectiveness.

Ruby on Rails Development – Features  To Develop

Ruby on Rails is best used when you are in need to develop following features for your website;

– E-commerce
– Membership
– Content Management
– Customized Databases
– Portfolios
– Coupons / Discounts
– Blog

Ruby on Rails Development – How to Use Instagram with MongoDB

If you want to use public posts from Instagram in your rails app the best way to do this is with Instagram gem and MongoDB.


Creating a new ruby on rails app

To learn it easily,  first of all, I will setup a new application to get Instagram public posts and I will save them into my local MongoDB database. This app is created with:

$ rails new instamongo –skip-active-record

Adding gems into your Gemfile.

gem ‘mongo’
gem ‘bson_ext’
gem “mongo_mapper”
gem “mongoid”
now run this from terminal
$ bundle install
and generate monoid.yml file with this

$ rails g mongoid:config
comment out this line in your application.rb file like this

#require “active_resource/railtie”
now we need create and save a tag so lets generate a scaffold for tag by
$ rails g scaffold tag name:string

Next step is Instagram configuration

To register your ruby on rails application, visit developer area of Instagram and get a new client id. Now, we need an access code so that our application can interact with Instagram API.

To get access, Ruby developer needs two things client_id and redirect url which is provided at the time of registration of your app on Instagram. Both of these items are available in Instagram developer area and look like this:[CLIENT_ID_HERE]&redirect_uri=[YOUR_REDIRECT_URL]&response_type=token

Then, you will be redirect to a URL like
http://YOUR_REDIRECT_ URL/#access_token=*****************************
You will find access token in query string, please copy and save this code.

Add this line into Gemfile

gem ‘instagram’
and run
$ bundle install

Now, create a new file config/initializer/instagram.rb and put these lines
require “instagram”
Instagram.configure do |config|
config.client_id = YOUR_CLINT_ID
config.access_token = YOUR_ACCESS_TOKEN

Now, create a model post in which we will save posts from Instagram. For now, I am going to save text, user profile image link and user name only so I will run this command from terminal.

$ rails g model post description:text profile_image_url:string user_name:string tag_id:symbol
now its time to create associations between tag and posts. So open your app/models/tag.rb and add this line
has_many :posts
and in app/models/tpost.rb
belongs_to :tag
def self.save_post(tag_id, text, image_url, screen_name)
post =
post.tag_id = tag_id
post.description = text
post.profile_image_url = image_url
post.user_name = screen_name
and update your create method of app/controllers/tags_controller.rb
def create
@tag =[:tag])

respond_to do |format|
@instagrams = Instagram.tag_recent_media(” “,””), {:count=>100})
@instagrams.each do |post|
text = post[“caption”][“text”] rescue “”
Post.save_post(, text, post[“user”][“profile_picture”], post[“user”][“username”])
format.html { redirect_to @tag, notice: ‘Tag was successfully created.’ }
format.json { render json: @tag, status: :created, location: @tag }
format.html { render action: “new” }
format.json { render json: @tag.errors, status: :unprocessable_entity }

In the above code I am passing a hash to Instagram.tag_recent_media in which ruby on rails developer can see :count=> 100 which will return 100 posts for a particular tag. By default instagram will return 20 posts for a tag.

Now start your server with
$ rails s

And visit http://localhost:3000/tags/new

Now, add a tag to get posts. Developers will be able to get posts in posts model. To see all these posts we need to generate a controller for post run like
$ rails g controller posts index

And add this
def index
@posts = Post.all
respond_to do |format|
format.json{render :json=> @posts }

Now, if you visit http://localhost:3000/posts/index.json you will have json data of all your posts. Instagram returns an array of hashes in which every hash refers to a post. Every post contains data like this

[“straycat”, “kedi”, “catlovers”, “instakedi”, “cat”, “catsagram”, “instacat”],
“width”=>306, “height”=>306
“width”=>150, “height”=>150
“width”=>640, “height”=>640
“text”=>”Kahvaltı^^ #kedi #instakedi #cat #instacat #catsagram #catlovers #straycat”,
“full_name”=>”Bilge”, “bio”=>””,

Using this method, ruby on rails developer can get their desired fields and values.

I always love to hear the feedback of my readers. Please leave your comments below.

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn