Posts

Showing posts with the label 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

Install Ruby on Rails on Mac

Please follow the below steps to i nstall RVM RUBY RAILS and Postgres in MAC yosemite. Step 1. Install Homebrew Homebrew is a package manager for the Mac. Installing Homebrew by issuing the following command: ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install) " To make sure Homebrew is installed correctly run: brew doctor Step 2: Install GPG Package. Download and install the GPG package from the link https://gpgtools.org Step 3: Install RVM gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 and \curl -sSL https://get.rvm.io | bash -s stable Reload your bash_profile, so it can initialize RVM. ~/.bashrc Run RVM requirements to install basic required packages with Homebrew. Accept the license agreement and install Xcode Tools. After installing the tools continue the rvm requirements installation. rvm requirements Step 4. Install Requir

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 :) 

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", "

Set up Jruby with apache and passenger in EC2

How to set up jruby with apache and passenger in amazone ec2. Step 1: First install jruby using please check the current version in the   link . And make sure you are installing the bin version of it wget 'http://jruby.org.s3.amazonaws.com/downloads/1.7.4/jruby-bin-1.7.4.tar.gz' Step 2: extract the tar file using tar zxvf jruby-bin-1.7.4.tar.gz Step 3: We need to set come environment variable so that it will be accessible from every other place. export JRUBY_HOME=/home/home_directory_name/jruby-1.7.4 export PATH=$JRUBY_HOME/bin:$PATH And Open the .bashrc file and put the path in their sudo nano ~/.bashrc PATH=$PATH:/home/ubuntu/jruby-1.7.4/bin Step 4: Now to check if jruby is properly installed or not put jruby-v If your system does not have java installed then it will show an error like /home/ubuntu/jruby-1.7.4/bin/jruby: line 395: exec: java: not found Don't worry we will install java to fix this. sudo apt-get install openjdk-7-jdk apt-cache

Set up ruby 2.0 and Rails 4.0.1 in Rackspace

First we need to install ruby Step 1)Install Ruby apt-get -y update apt-get -y install build-essential zlib1g-dev libssl-dev libreadline6-dev libyaml-dev cd /tmp wget http://cache.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p247.tar.gz tar -xvzf ruby-2.0.0-p247.tar.gz cd ruby-2.0.0-p247/ ./configure --prefix=/usr/local make make install Step 2) Install Rails gem install rails 4.0.1 Step 3) Install passenger gem install passenger Step 4) Install Apache2 apt-get install apache2 Step 5) Install Mysql sudo apt-get install mysql-server mysql-client That's it we are done with the setup.
In order to upload multiple file to the server the basic thing what we require is multiple file fields. we can do it easily in .erb file with the help of javascript. So just follow the steps and have fun. Step 1 :  create a simple html div for it.                 <div class="apply_div  " id="number_of_download_filed" >        <div class="attachment_div_text" id='div_attached_1'>Add Attachment,</div> <div class="attachment_div_field" id="div_attached_field_1"> <input id='attached_1' type='file' name='attached_1' class='event_action_attach_fields input_file_class'> </div>             </div>            <div class="apply_div ">                              <%= button_tag ' Attach another file ',  :type => 'button',  : class="attach_another_file_button"   %>             </div>