Posts

Showing posts with the label ruby on rails

Insert Bulk data using active record import

Imagine a scenario where you have to create a report and send it to multiple clients, so lets just say you have a report model and you have client and you have report client model. lets first check all the association and model structure. #report model class Report < ActiveRecord::Base   has_many :report_clients   has_many :clients, through: :report_clients end #report_client class ReportClient < ActiveRecord::Base   belongs_to :report   belongs_to :client end #clients class Client < ActiveRecord::Base   has_many :report_clients   has_many :reports, through: :report_clients end No lets say you want to create a report for 30 clients. You can do that by adding nested attributes in rails. so our report model will became  class Report < ActiveRecord::Base   has_many :report_clients   has_many :clients, through: :report_clients   accepts_nested_attributes_for :report_clients, :reject_if => proc { |attributes| att

Insert Bulk data using active record import

Imagine a scenario where you have to create a report and send it to multiple clients, so lets just say you have a report model and you have client and you have report client model. lets first check all the association and model structure. #report model class Report < ActiveRecord::Base   has_many :report_clients   has_many :clients, through: :report_clients end #report_client class ReportClient < ActiveRecord::Base   belongs_to :report   belongs_to :client end #clients class Client < ActiveRecord::Base   has_many :report_clients   has_many :reports, through: :report_clients end No lets say you want to create a report for 30 clients. You can do that by adding nested attributes in rails. so our report model will became  class Report < ActiveRecord::Base   has_many :report_clients   has_many :clients, through: :report_clients   accepts_nested_attributes_for :report_clients, :reject_if => proc { |attributes| att

Create Nested form In Ruby on Rails 4.1

In Rails we create simple form and also sometimes we need nested form. In Rails we can easily do that using  gem “nested_form”. let's create a project using “nested_form”. Before that I would like to ask you to have a look on nested_attribute_documentation(http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#method-i-accepts_nested_attributes_for). 1) Create a new Rails 4 Project.     rails new nested_app 2) Include the gem in your gem file.   gem "nested_form" 3) run bundle install  bundle install 4) Generate the necessary javascript file.   rails g nested_form:install 5) Include the javascript file in the asset pipeline. in the application.js add the below line.   //= require jquery_nested_form 6) Let say we have a Exam model and one Exam can have multiple Questions.So lets create the Exam model. class Exam < ActiveRecord::Base   has_many :questions, dependent: :destroy   accepts_nested_attributes_for :questions,

How To Setup Ruby on Rails with Postgres

Postgres is a great open source database management system. It's pretty easy to use postgres with Ruby on Rails. Step 1: Install postgress in your system. If you already have postgres installed in your system then skip it else follow the  link . Step 2: Create new project rails new pg_demo You can also create using rails new pg_demo --database=postgresql Step 3: include the pg gem in your gem file gem 'pg' Step 4: run bundle command bundle Step 5: It will give you some error like An error occurred while installing pg (0.17.1), and Bundler cannot continue. Make sure that `gem install pg -v '0.17.1'` succeeds before bundling. So let's try to install the  pg gem first gem install pg It will again throw an error  :( Building native extensions.  This could take a while... ERROR:  Error installing pg: ERROR: Failed to build gem native extension.     /home/sabyasachi/.rvm/rubies/ruby-2.1.0/bin/ruby extconf.rb checking for pg_config... y

How to do rake db:seed in using neo4j gem in rails 4

Neo4j is a graph database, using neo4j gem( https://github.com/neo4jrb/neo4 j) we can easily use neo4j database in our ruby on rails application ( http://programminginrubyonrails.blogspot.in/2014/12/neo-4j-with-rails.html ). Till now neo4j gem does not support rake db tasks. You can check all the supported task by typing rake -T in your console. Now we can create our own custom rake task to seed some  data in neo4j . Step 1: Create a file under lib/tasks name it seed.rake Step 2: Put the below code in seed.rake. namespace :db do   desc 'Load the seed data from db/seeds.rb'   task :seed => :environment do     seed_file = File.join(Rails.root, 'db', 'seeds.rb')     load(seed_file) if File.exist?(seed_file)   end end That's it. now you can run rake db:seed from your terminal.. Simple :) 

Set up Neo 4j With Rails In Ubuntu

How to setup new 4j database with Rails 4. Step 1: Install Neo4j Database in ubuntu.   login as Admin  sudo -i  wget -O - http://debian.neo4j.org/neotechnology.gpg.key | apt-key add -  Add Neo4J to the Apt sources list:   echo 'deb http://debian.neo4j.org/repo stable/' > /etc/apt/sources.list.d/neo4j.list  Update the package manager:  exit as admin  apt-get update  Install Neo4J:  apt-get install neo4j  sudo /etc/init.d/neo4j-service restart Step 2: Create a new rails project.  Rails new neo4j Step 3:  Add neo4j Gem in your gem file   gem 'neo4j', github: 'andreasronge/neo4j' Step 4: Go to application.rb require File.expand_path('../boot', __FILE__) require "rails" %w(   neo4j   action_controller   action_mailer   sprockets ).each do |framework|   begin     require "#{framework}/railtie"   rescue LoadError   end end # Require the gems listed in Gemfile, including any gems # you've

Creating RESTful API using Grape and RABL in Rails 4

We can create a RESTful API using Grape and RABL in Rails4. Step 1: Add this GEMS in your Gem file, And do Bundle install.  gem 'grape'  gem 'rabl-rails'  gem 'grape-rabl'  gem 'rack-cors', :require => 'rack/cors' Step 2: Create a file under lib directory called api.rb, and create a folder inside the view named as api. # lib/api/v1/root.rb class API < Grape::API end Step 3: Add this line to your application.rb file    config.paths.add "app/api", glob: "**/*.rb"     config.autoload_paths += %W(#{config.root}/lib #{config.root}/app/api/*)     config.middleware.use Rack::Cors do       allow do         origins '*'         # location of your API         resource '/*', :headers => :any, :methods => [:get, :post, :options, :put, :delete]       end     end     config.middleware.use(Rack::Config) do |env|       env['api.tilt.root'] = Rails.root.join "app", "

sunspot_rails the Sunspot library for Solr search.

Now its very easy to integrate solar search using sunspot_rails gem in rails 3 for fast searching.  Please follow the below steps to use solar search. 1) first install the gem    gem 'sunspot_rails'    gem 'sunspot_solr'   # optional pre-packaged Solr distribution for use in development 2) Run the bundle command to install the gems. 3) Run      rails generate sunspot_rails:install      to create the configuration file for the solar search. 4) Now you need to start the solar server     bundle exec rake sunspot:solr:start 5) If you have some exiting data in the data base please execute the command     rake sunspot:reindex 6) Now we are done with our setup.we need to configure our model so that search will work.    let say we have one event model in our application like    class Event < ActiveRecord::Base       attr_accessible  :content, :title     end Now if we want a full text search on the Event model. We just need to add one block in t

mongo.js:L112 Error: couldn't connect to server 127.0.0.1:27017 atsrc/mongo/shell/mongo.js:L112

mongo.js:L112 Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:L112 Stop mongodb service sudo service mongodb stop Remove mongodb lock file sudo rm /var/lib/mongodb/mongod.lock Change ownership from root to mongodb path sudo chown -R mongodb:mongodb /var/lib/mongodb/ Start mongodb service sudo service mongodb start Test mongo app mongo Then you will be able to execute successfully (i hope).

Rmagic installation problem in rails 3

How to install ImageMagick  In Rails 3 Before installing r magic please installed the required library in your system. please follow the Steps. sudo apt-get install imagemagic sudo apt-get install libmagickcore-dev sudo apt-get install l ibmagickwand-dev After successful installation of the above 3 libraries now run gem install 'rmagick' Now rmagic will install successfully. :)

How to write stored procedure in rails 3

Writing Stored procedure in rails 3 Now using Mysql2 gem writing stored procedure is pretty much easy. Just we need to follow the below steps to write  stored procedure in ORM. 1) Connect the database. client = Mysql2::Client.new(:host => "localhost", :username => "root", :database => "XXX", :password => "XYZ", :flags => Mysql2::Client::MULTI_STATEMENTS ) This extra flag option is most important and multiple result sets is normally used when calling stored procedures that return more than one result set. 2) Fetch the data. result  = client.query( 'CALL sp_test'); It will return the Result set. Now result contains first result set and to fetch the Next result set we need to follow the next step While (client.next_result)    result = client.store_result  #To get the next result set end

Rails 3 ActionDispatch::Cookies::CookieOverflow

ActionDispatch :: Cookies :: CookieOverflow ( ActionDispatch :: Cookies :: CookieOverflow ): Problem in rails 3 devise. The problem is with session["devise.facebook_data"] = env["omniauth.auth"] . Twitter's response contains an extra section that is very large and does not fit in the session. One option is to store env["omniauth.auth"].except("extra") in the session instead. This is issue arises where we are storing more than 4K of session data.