Posts

Showing posts with the label active record

some minor things in rails

For Date format  .utc.strftime('%d-%m-%Y %H:%M:%S') Alternative to html_safe (raw("<strong class='text-danger'>Decline</strong")). select tag for form_for  <%= f.select(:currency_id, options_for_select(@currencies.map{|x| x.id}, f.object.payment_currency), {include_blank: "Select Currency"}, { :class => 'form-control', required: true }) %> zip -r new_file_name.zip folder_name

Create worker in rails by sidekiq and redis

Add sidekiq to your Gemfile: Add this to your Gemfile gem 'sidekiq' Run bundle install bundle install Create worker rails g sidekiq:worker Test #will create app/workers/test_worker.rb class TestWorker   include Sidekiq::Worker   def perform(id, name)     # do something   end end Call worker without any time boundation TestWorker.perform_async(5, 'rahul') //use this anywhere from your rails app(controller, model, helpers..) Call worker within particular time period TestWorker.perform_in(5.minutes, 5, 'rahul')  //use this anywhere from your rails app(controller, model, helpers..) Call worker after specific time interval once only TestWorker.perform_at(5.minutes.from_now, 5, 'rahul')  //use this anywhere from your rails app(controller, model, helpers..) Start sidekiq on development bundle exec sidekiq Start sidekiq on production bundle exec sidekiq -d //run this first if you get some error then use second one bundle exec si

Custom order of Record | | Order as specified || order_as_specified

Custom order of Record | | Order as specified || order_as_specified Install gem gem 'order_as_specified' bundle install or gem install order_as_specified write this line at top of your model / /that model you have to fetch record in custom order extend OrderAsSpecified Now in your controller method ids=[1,3,5,6,7,4,2] // assuming you have 7 record with this id's & and you want records in this order then Model.where(id: ids).order_as_specified(id: ids) //this will give you record in custom order Here is Working Example def index    premia1 = []    premia2 = []    PremiumInstruction.all.select{|pi| premia1 << pi.premium}    Premium.all.select{|p| premia2 << p if p.premium_instructions.blank?}    permia = premia2 + premia1.uniq //add both array    ids = permia.pluck(:id) //getting ids from array into an array //learn about pluck in other post    @collection = Premium.where(id: ids). order_as_specified (id: ids) end Here we