jeudi 31 mai 2018

Active record expression to update values from another source?

I have the following data sources:

@current_products = Product.where(store_id: @current_store) # local db
@new_products = NewProduct.where(store_id: @new_store) # remote db

Customers update product_name within @new_products and I need those names to be updated within @current_products. A product will have a product_id that is the same on both @current_products and @new_products that I can use as a key.

I am starting off with:

product_ids = []
@current_products.each do |item|   
        product_ids << item[:product_id]
end  

but then I'm not sure how update the respective product_title within @current_products:

@new_products.each do |item|               
# code to update Product with respective product_id from NewProduct    

end

Should I be updating using something like the following?

product = Product.find_or_create_by(<something here>)
product.update_attributes(<something here>)

I'm trying to get my head around: https://apidock.com/rails/ActiveRecord/Base/update_all/class but I cannot find a relevant example for my case.

mardi 29 mai 2018

how add address to users with Google map and jt-rails-address?

I'm new on rails. I work on a project, and I try to add physical address to my users. I want that the address can be help-complete with Google map for later exploitation. I find the jt-rails-address wich look like perfect for my project. But I can't implement it. I need complete address (street, zip code, city & country).

add_address_to_users.rb :

class AddAddressToUsers < ActiveRecord::Migration[5.1]
 def change
  add_column :users, :address, :address
 end
end

form edit.html.erb :

  <div class="col-md-6 col-md-offset-3">
  <%= form_for (@user), :html => { :multipart => true } do |f| %>
  <%= render 'shared/error_messages' %>

  <%= f.label :phone, "Téléphone :" %>
  <%= f.phone_field :phone, class: 'form-control' %>

  <%= f.label :address, "Addresse :" %>
  <div class="jt-address-autocomplete">
    <!-- This field is used to search the address on Google Maps -->
    <%= f.text_field :address, class: 'jt-address-search' %>

    <!-- All fields are hidden because the javascript will set their value automatically -->
    <% for attr in JT::Rails::Address.fields %>
    <%= f.hidden_field "address_#{street}", class: "jt-address-field-#{street}" %>
    <% end %>

    <% for attr in JT::Rails::Address.fields %>
    <%= f.hidden_field "address_#{zip_code}", class: "jt-address-field-#{zip_code}" %>
    <% end %>

    <% for attr in JT::Rails::Address.fields %>
    <%= f.hidden_field "address_#{city}", class: "jt-address-field-#{city}" %>
    <% end %>


    <% for attr in JT::Rails::Address.fields %>
    <%= f.hidden_field "address_#{country}", class: "jt-address-field-#{country}" %>
    <% end %>

   </div>

    <%= f.submit "Enregistrer les changements", class: "btn btn-primary" %>
   <% end %>
  </div>

application.js :

// This function is call when Google Maps is loaded
window.googleMapInitialize = function(){

// Simple usage
$('.jt-address-autocomplete').jt_address();

 };

I have already put :address in my user_params in the users controller and has_address :address in the user model I also have put my Google Api.

My actual error :

undefined local variable or method `street' for #<#:0x00007fe91dfaefc0>

Thanks in advance for your help.

how to send file's binary to params and save it in DB with paperclip_database

Sorry for maybe newbie question. I want to use a paperclip and paperclip_database gems to attach and save files in my database. But I stuck on sending file's data from views to controller. I've done all from this, and this resources. As the result I have next models:

class User < ActiveRecord::Base
  has_many :certificates, :dependent => :destroy
  accepts_nested_attributes_for :certificates, :allow_destroy => true
end
class Certificate < ActiveRecord::Base
  belongs_to :user

  attr_accessor :image
  has_attached_file :image,
                    :storage => :database,
                    :database_table => 'image_files',
                    :cascade_deletion => true
end

in a controller

Users_controller
  def new
    @user = User.new
    @user.certificates.build
    ~~~~~~~~~~~~~~~~
    ~~~~~~~~~~~~~~~~
  end

  def create
    @user = User.new(params[:user])
    ~~~~~~~~~~~~~~~~
    ~~~~~~~~~~~~~~~~
  end
end

and my view form is:

<%= form_tag @user, method: :put, :html => { :multipart => true } do |f|%>
  <%= fields_for each_event_entry.certificates.first do |c| %>
    <tr>
      <th>Certificate</th>
      <td>
        <%= c.file_field :image %>
      </td>
    </tr>
  <% end %>
<% end %>

But when I attach a file and try to submit, I've only got a file name in params:

{
 "user" => {"some"=>"params"}
 "certificate"=>{"image"=>"IMG_1642.JPG"}
 }

And certificate is saved whithout attached file. Any help will be extremely appreciated.

How to use meta tag in angular js with ruby on rails

I am using the angular js version 1.5 with ruby on rails and how to use meta tag with ruby on rails in angular js.

lundi 28 mai 2018

Rails 5.2: GCP deployment fails after starting to use the new encrypted credentials feature

I'm facing quite a strange issue with GCP (Google Cloud Platform) deployments. I've recently updated my Ruby on Rails application to 5.2 (was 5.1.4 previously) and I started using the the encrypted credentials feature (which I believe is amazing, to be honest), but it looks like GCP is not that happy with it.

Here is the traceback that I receive when I deploy (via gcloud from my local workstation and also CircleCI)

Started cloud_sql_proxy.
D, [2018-05-28T16:19:42.241746 #17] DEBUG -- : Cannot load `Rails.application.database_configuration`:
ActiveSupport::MessageEncryptor::InvalidMessage excluded from capture: DSN not set

rake aborted!
ActiveSupport::MessageEncryptor::InvalidMessage: Cannot load `Rails.application.database_configuration`:
ActiveSupport::MessageEncryptor::InvalidMessage
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/message_encryptor.rb:206:in `rescue in _decrypt'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/message_encryptor.rb:183:in `_decrypt'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/message_encryptor.rb:157:in `decrypt_and_verify'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/messages/rotator.rb:21:in `decrypt_and_verify'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/encrypted_file.rb:79:in `decrypt'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/encrypted_file.rb:42:in `read'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/encrypted_configuration.rb:21:in `read'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/encrypted_configuration.rb:33:in `config'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/encrypted_configuration.rb:38:in `options'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/core_ext/module/delegation.rb:271:in `method_missing'
(erb):25:in `<main>'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.2.0/lib/rails/application/configuration.rb:172:in `database_configuration'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.2.0/lib/active_record/railtie.rb:133:in `block (2 levels) in <class:Railtie>'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/lazy_load_hooks.rb:71:in `instance_eval'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/lazy_load_hooks.rb:71:in `block in execute_hook'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/lazy_load_hooks.rb:62:in `with_execution_control'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/lazy_load_hooks.rb:67:in `execute_hook'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/lazy_load_hooks.rb:43:in `block in on_load'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/lazy_load_hooks.rb:42:in `each'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/lazy_load_hooks.rb:42:in `on_load'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.2.0/lib/active_record/railtie.rb:132:in `block in <class:Railtie>'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.2.0/lib/rails/initializable.rb:32:in `instance_exec'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.2.0/lib/rails/initializable.rb:32:in `run'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.2.0/lib/rails/initializable.rb:61:in `block in run_initializers'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.2.0/lib/rails/initializable.rb:60:in `run_initializers'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.2.0/lib/rails/application.rb:361:in `initialize!'
/app/config/environment.rb:5:in `<main>'
/app/vendor/bundle/ruby/2.3.0/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'
/app/vendor/bundle/ruby/2.3.0/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
/app/vendor/bundle/ruby/2.3.0/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/loaded_features_index.rb:65:in `register'
/app/vendor/bundle/ruby/2.3.0/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
/app/vendor/bundle/ruby/2.3.0/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:283:in `block in require'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:249:in `load_dependency'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:283:in `require'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.2.0/lib/rails/application.rb:337:in `require_environment!'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.2.0/lib/rails/application.rb:520:in `block in run_tasks_blocks'
/app/vendor/bundle/ruby/2.3.0/gems/sprockets-rails-3.2.1/lib/sprockets/rails/task.rb:62:in `block (2 levels) in define'
/app/vendor/bundle/ruby/2.3.0/gems/rake-12.3.1/exe/rake:27:in `<top (required)>'
/rbenv/versions/2.3.7/bin/bundle:22:in `load'
/rbenv/versions/2.3.7/bin/bundle:22:in `<main>'

Caused by:
OpenSSL::Cipher::CipherError: key length too short
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/message_encryptor.rb:193:in `key='
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/message_encryptor.rb:193:in `_decrypt'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/message_encryptor.rb:157:in `decrypt_and_verify'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/messages/rotator.rb:21:in `decrypt_and_verify'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/encrypted_file.rb:79:in `decrypt'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/encrypted_file.rb:42:in `read'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/encrypted_configuration.rb:21:in `read'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/encrypted_configuration.rb:33:in `config'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/encrypted_configuration.rb:38:in `options'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/core_ext/module/delegation.rb:271:in `method_missing'
(erb):25:in `<main>'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.2.0/lib/rails/application/configuration.rb:172:in `database_configuration'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.2.0/lib/active_record/railtie.rb:133:in `block (2 levels) in <class:Railtie>'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/lazy_load_hooks.rb:71:in `instance_eval'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/lazy_load_hooks.rb:71:in `block in execute_hook'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/lazy_load_hooks.rb:62:in `with_execution_control'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/lazy_load_hooks.rb:67:in `execute_hook'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/lazy_load_hooks.rb:43:in `block in on_load'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/lazy_load_hooks.rb:42:in `each'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/lazy_load_hooks.rb:42:in `on_load'
/app/vendor/bundle/ruby/2.3.0/gems/activerecord-5.2.0/lib/active_record/railtie.rb:132:in `block in <class:Railtie>'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.2.0/lib/rails/initializable.rb:32:in `instance_exec'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.2.0/lib/rails/initializable.rb:32:in `run'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.2.0/lib/rails/initializable.rb:61:in `block in run_initializers'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.2.0/lib/rails/initializable.rb:60:in `run_initializers'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.2.0/lib/rails/application.rb:361:in `initialize!'
/app/config/environment.rb:5:in `<main>'
/app/vendor/bundle/ruby/2.3.0/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `require'
/app/vendor/bundle/ruby/2.3.0/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in `block in require_with_bootsnap_lfi'
/app/vendor/bundle/ruby/2.3.0/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/loaded_features_index.rb:65:in `register'
/app/vendor/bundle/ruby/2.3.0/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in `require_with_bootsnap_lfi'
/app/vendor/bundle/ruby/2.3.0/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in `require'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:283:in `block in require'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:249:in `load_dependency'
/app/vendor/bundle/ruby/2.3.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:283:in `require'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.2.0/lib/rails/application.rb:337:in `require_environment!'
/app/vendor/bundle/ruby/2.3.0/gems/railties-5.2.0/lib/rails/application.rb:520:in `block in run_tasks_blocks'
/app/vendor/bundle/ruby/2.3.0/gems/sprockets-rails-3.2.1/lib/sprockets/rails/task.rb:62:in `block (2 levels) in define'
/app/vendor/bundle/ruby/2.3.0/gems/rake-12.3.1/exe/rake:27:in `<top (required)>'
/rbenv/versions/2.3.7/bin/bundle:22:in `load'
/rbenv/versions/2.3.7/bin/bundle:22:in `<main>'
Tasks: TOP => environment

The first error (DSN not set) is most likely coming from Sentry, although the DSN was added to the encrypted credentials file and Sentry if configured correctly.

config.dsn = Rails.application.credentials.dig(:sentry, :staging, :raven_dsn)

Second error (Cannot load Rails.application.database_configuration) makes me think that it's something related to database.yml, which works just fine in development and test environments.

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000

development:
  <<: *default
  database: gcpfive_development

test:
  <<: *default
  database: gcpfive_test

staging:
  <<: *default
  username: <%= Rails.application.credentials.dig(:gcsql, :staging, :username) %>
  password: <%= Rails.application.credentials.dig(:gcsql, :staging, :password) %>
  database: <%= Rails.application.credentials.dig(:gcsql, :staging, :database) %>
  host: <%= Rails.application.credentials.dig(:gcsql, :staging, :host) %>

The third error (OpenSSL::Cipher::CipherError: key length too short) hints that maybe the master key is not found during the deploy and maybe that's why it also can't find the Sentry DSN? Has anyone else encountered a similar issue?

rails application is not working with https

I have set up my application on the server using apache + passenger (mod rails). I have also setup Capistrano to deploy the application. The application is up and running now and I am able to access it using IP/host (as the domain is not configured yet) Now I have updated the application.conf file to enable the SSL so it would also run on https scheme however after this configuration the application is down and be giving error "the requested URL is not found" apache/2.2.15 red hat server at ip on port 443"

any idea whats wrong with the configuration. It is working with HTTP if I removed the SSL configuration

How to use create! method on a class with a constructor that takes 1 argument

I have a class look like this (simplified)

class Timereg < ActiveRecord::Base

  def initialize(hour_id)
    super()
    self.hour_id = hour_id
    self.status = -2
    self.slug = SecureRandom.uuid.to_s
  end
end

When using it like this

Timereg.new(1)

All good

But how can I use it with this

Timereg.create!

I cant figure out the syntax I keep getting

ArgumentError: wrong number of arguments (2 for 1)

dimanche 27 mai 2018

Rails: More efficient way of my Select and Each loop?

I currently use the following loops to cycle through a product list. If the item[:product_id] exists in my Products table then I want to grab the length, width and height value. What I currently have is:

   params[:items].each do |item|  
      item = item[:product_id]
       productDimensions = Product.select(:length, :width, :height).where(product_id: item)
      productDimensions.each do |product|  
        return length = product.length
        return width = product.width          
        return height = product.height
      end
   end

Now this works, but it feels like a very cumbersome way to do it.

Is there a better way I can be selecting these values?

samedi 26 mai 2018

how can i link to a tag using tag_counts from acts_as_taggable?

I am using acts_as_taggable to add tags to a post in order to be able to show all posts with the specific tag. This worked fine for me with:

  #in posts_controller.rb
def index
if params[:tag].present?
  @posts = Post.tagged_with(params[:tag]).paginate(:page => params[:page])
else
  @posts = Post.all.order("created_at DESC")
end
end

and displaying them in my index.html.erb with

<p> tags: <%= raw post.tags.map{ |t| link_to t.name, posts_path(tag: t.name)}.join(', ')  %><p>

Now i want to display all tags in another view with <%=raw Post.tag_counts_on(:tags).map{|t| link_to t.name, posts_path(tag: t.name)}%>

and faced the problem that t.name is no longer just the tag name but "/t.name\" which is linking me to the wrong site.

Is there a way to solve this problem or maybe even a better solution to link all tags that i got?

vendredi 25 mai 2018

bcrypt error in rails - LoadError in Devise

I am running rails in windows 10 and when I try to log in I'm getting bcrypt load error. I used to get this error before but I used to solve it. But this time I am not able to. I have tried all the solution by installing bcyrpt gem but nothing is working for me. I am stuck with this. Can anybody help me

I have referred this before

Ruby on windows causes error Cannot load such file bcrypt_ext

jeudi 24 mai 2018

Ruby List liker for IG won't click Log In before proceeding

My names Adam I'm studying Applied Computing in college. I'm currently trying to use Ruby to run a list liker for instagram, right now it will enter my log in details but won't click the button before trying to proceed and I don't know how to fix it, im really new to this programming malarchy. any help would be greatly appreciated [The code in question1

Gem::Ext::BuildError: ERROR: Failed to build gem native extension for rails version 5.2.0

I cloned a ruby on rails app and try to run bundle install here is the full error what i got while running the app :

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

C:/Ruby22/bin/ruby.exe extconf.rb
creating Makefile

make "DESTDIR=" clean

make "DESTDIR="
generating generator-i386-mingw32.def
compiling generator.c
In file included from c:/Ruby22/include/ruby-2.2.0/ruby.h:33:0,
                 from ../fbuffer/fbuffer.h:5,
                 from generator.c:1:
c:/Ruby22/include/ruby-2.2.0/ruby/ruby.h:118:14: error: size of array 'ruby_check_sizeof_voidp' is negative
In file included from c:/Ruby22/include/ruby-2.2.0/ruby.h:33:0,
                 from ../fbuffer/fbuffer.h:5,
                 from generator.c:1:
c:/Ruby22/include/ruby-2.2.0/ruby/ruby.h: In function 'rb_obj_freeze_inline':
c:/Ruby22/include/ruby-2.2.0/ruby/ruby.h:1171:9: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
c:/Ruby22/include/ruby-2.2.0/ruby/ruby.h:1172:2: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
c:/Ruby22/include/ruby-2.2.0/ruby/ruby.h:1173:6: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
c:/Ruby22/include/ruby-2.2.0/ruby/ruby.h:1173:27: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
c:/Ruby22/include/ruby-2.2.0/ruby/ruby.h: In function 'rb_data_object_get':
c:/Ruby22/include/ruby-2.2.0/ruby/ruby.h:1207:13: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
c:/Ruby22/include/ruby-2.2.0/ruby/ruby.h: In function 'rb_obj_written':
c:/Ruby22/include/ruby-2.2.0/ruby/ruby.h:1286:10: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
c:/Ruby22/include/ruby-2.2.0/ruby/ruby.h:1286:34: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
c:/Ruby22/include/ruby-2.2.0/ruby/ruby.h: In function 'rb_num2char_inline':
c:/Ruby22/include/ruby-2.2.0/ruby/ruby.h:1359:9: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
c:/Ruby22/include/ruby-2.2.0/ruby/ruby.h:1359:9: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
c:/Ruby22/include/ruby-2.2.0/ruby/ruby.h:1359:9: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
c:/Ruby22/include/ruby-2.2.0/ruby/ruby.h:1359:36: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
c:/Ruby22/include/ruby-2.2.0/ruby/ruby.h:1359:36: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
c:/Ruby22/include/ruby-2.2.0/ruby/ruby.h:1359:36: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
c:/Ruby22/include/ruby-2.2.0/ruby/ruby.h:1360:9: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
c:/Ruby22/include/ruby-2.2.0/ruby/ruby.h:1360:9: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
c:/Ruby22/include/ruby-2.2.0/ruby/ruby.h:1360:9: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
c:/Ruby22/include/ruby-2.2.0/ruby/ruby.h: In function 'rb_class_of':
c:/Ruby22/include/ruby-2.2.0/ruby/ruby.h:1685:12: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
c:/Ruby22/include/ruby-2.2.0/ruby/ruby.h: In function 'rb_type':
c:/Ruby22/include/ruby-2.2.0/ruby/ruby.h:1702:12: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
In file included from generator.c:1:0:
../fbuffer/fbuffer.h: In function 'fbuffer_append_str':
../fbuffer/fbuffer.h:127:25: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
../fbuffer/fbuffer.h:127:25: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
../fbuffer/fbuffer.h:127:25: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c: In function 'convert_UTF8_to_JSON_ASCII':
generator.c:135:35: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:135:35: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:135:35: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:136:38: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:136:38: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:136:38: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c: In function 'convert_UTF8_to_JSON':
generator.c:233:23: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:233:23: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:233:23: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:234:25: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:234:25: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:234:25: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c: In function 'cState_configure':
generator.c:572:15: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:572:15: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:572:15: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:573:34: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:573:34: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:573:34: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:580:15: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:580:15: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:580:15: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:581:33: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:581:33: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:581:33: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:588:15: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:588:15: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:588:15: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:589:40: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:589:40: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:589:40: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:596:15: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:596:15: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:596:15: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:597:36: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:597:36: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:597:36: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:604:15: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:604:15: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:604:15: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:605:37: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:605:37: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:605:37: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c: In function 'set_state_ivars':
generator.c:653:21: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:653:21: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:653:21: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:655:24: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:655:24: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:655:24: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c: In function 'generate_json_object':
generator.c:740:20: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:740:20: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:740:20: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c: In function 'generate_json_array':
generator.c:786:20: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:786:20: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:786:20: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c: In function 'generate_json':
generator.c:892:16: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:892:16: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:892:16: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c: In function 'isArrayOrObject':
generator.c:953:23: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:953:23: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:953:23: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:954:15: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:954:15: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:954:15: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c: In function 'cState_indent_set':
generator.c:1078:11: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:1078:11: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:1078:11: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:1087:32: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:1087:32: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:1087:32: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c: In function 'cState_space_set':
generator.c:1116:11: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:1116:11: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:1116:11: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:1125:31: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:1125:31: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:1125:31: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c: In function 'cState_space_before_set':
generator.c:1152:11: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:1152:11: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:1152:11: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:1161:38: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:1161:38: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:1161:38: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c: In function 'cState_object_nl_set':
generator.c:1190:11: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:1190:11: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:1190:11: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:1198:35: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:1198:35: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:1198:35: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c: In function 'cState_array_nl_set':
generator.c:1225:11: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:1225:11: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:1225:11: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:1233:34: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:1233:34: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
generator.c:1233:34: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
make: *** [generator.o] Error 1

make failed, exit code 2

I try alot to solve this error but after every try getting same error. Kindly send me some solutions for same,Thanks in Advance.

mercredi 23 mai 2018

vagrant up not working with lxc provider

I have issues running vagrant up, i am using lxc provider. i get this error: There was an error executing ["sudo", "lxc-start", "-d", "--name", "work_lxc_done_trusty_dev_152699742559_00608"]. versions : vagrant 1.9.4 lxc: 1.4.0 ubuntu 16.04

how to prevent SQL injection in ruby on rails with post request

I am getting a security error in my ROR code that SQL injection may be possible in one post request when I run The OWASP Zed Attack Proxy (ZAP) tool on my code. I have a get_auth_token method which does not take any parameters , ZAP tool adds a query parameter in this request and on executing the modifier request the page loads fine which is an issue

Below is the modified url with added query param

https://example.com/api/sessions/get_auth_token?query=query+AND+1%3D1+--+

get_auth_token implementation looks like this

def get_auth_token
  user = User.find_by(id: session[:user_id], disabled: false) 
 if session[:user_id]
  if user
  token = AuthToken.issue(user_id:user.username)
  session[:user_id] = user.id
  session[:username] = user.username
  session[:project_id] = user.get_setting('xyz', nil)
  User.update(user.id, :token=>token)

I do not want to convert this request to GET. I have gone through many examples for SQL injection prevention , they all focus on methods which accept some param. My method does not accept any param. How can I make sure that it does not accept any external param to prevent this SQL injection

mardi 22 mai 2018

We are looking for an instructor for a Front End Web Development, Ruby on Rails Class in Middletown, DE.

We are looking for an instructor for a Front End Web Development, Ruby on Rails Class in Middletown, DE. Please give me a call at 888-579-3758 or email jcangemi@condensedcurriculum.com So we can discuss the opportunity.

Modeling has_one "active" child among has_many children

I have a model:

class Group < ActiveRecord::Base
  has_many :progressions
  has_one :active_progression, class_name: "Progression"
end

And the child:

class Progression < ActiveRecord::Base
  belongs_to :group
end

I know that convention says I should create a separate foreign key inside Progression to associate the active progression with the Group's id (as that is how has_one works).

However, IMHO, it makes more logical sense to store the 'active' child's id in the Group model. Edit: The reasoning is that storing the Group id in the Progression model as active_group_id or something similar is semantically confusing and opens the possibility of having multiple active progressions associated with a Group, which is obviously incorrect.

I have read several posts where people confuse the underlying has_one meaning, assuming it does the same thing as belongs_to but for the parent. I know this is not the case. has_one does the opposite of belongs_to, i.e. looking for the key in the counterpart instead of the calling model.

If Rails does not provide a method for creating an association of this nature, what is the most Railsy solution?

lundi 21 mai 2018

infer/add type_task from polymorphic child model

I have a parent class task and severals children tasks types something like this:

class Task
  field :name, type: String
  field :description, type: String
end

class Phase
  has_many :tasks, as: :taskable
end

class TaskManager < Task
 belongs_to :taskable, polymorphic: true
end

class TaskEmployee < Task
 belongs_to :taskable, polymorphic: true
end

When i'm creating a new task i would like add to task parent class a task_type children object.

How can i infer/add task_type object to parent task?

Thank you!

dimanche 20 mai 2018

Ruby on Rails server cannot connect to mysql

everyone. I am trying to set up rails 3 project with mysql as database on macOS X. However, I receive the next error:

/usr/local/bundle/gems/activerecord-3.2.22.5/lib/active_record/connection_adapters/abstract/connection_specification.rb:68:in connection_url_to_hash': undefined method `sub' for nil:NilClass (NoMethodError)

config/database.yml

default: &default
  adapter: mysql2
  pool: 5
  encoding: utf8
  min_messages: warning
  timeout: 5000
  username: root
  password:
  database: heart_development

Can anyone suggest how to solve this problem?

Error while running ruby server on Gitbash

I am new to Ruby on Rails and I was trying to learn it using the youtube videos and I have installed Heroku, Postgre SQL, Git Bash and trying to run the Rails Server and getting the following error.

Can you guys help me out on this? There was similar issue posted here I tried using the resolution provided there but it did not work.

  • Tried uncommening the # gem 'therubyracer', platforms: :ruby that did not work

  • People mentioned that we need to install Nodejs although I have nodejs in my system I am not using it should I install it even though I am not going to use it?

Please help me on this.

$ rails s

c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:85:in rescue in block (2 levels) in require': There was an error while trying to load the gem 'uglifier'. (Bundler::GemRequireError) Gem Load Error is: Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes. Backtrace for gem load error is: c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/execjs-2.7.0/lib/execjs/runtimes.rb:58:inautodetect' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/execjs-2.7.0/lib/execjs.rb:5:in <module:ExecJS>' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/execjs-2.7.0/lib/execjs.rb:4:in' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/uglifier-4.1.10/lib/uglifier.rb:5:in require' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/uglifier-4.1.10/lib/uglifier.rb:5:in' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:82:in require' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:82:inblock (2 levels) in require' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:77:in each' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:77:inblock in require' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:66:in each' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:66:inrequire' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.15.3/lib/bundler.rb:108:in require' c:/Sites/myrubyblog/config/application.rb:7:in' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:133:in require' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:133:inblock in perform' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:130:in tap' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:130:inperform' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/thor-0.20.0/lib/thor/command.rb:27:in run' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/thor-0.20.0/lib/thor/invocation.rb:126:ininvoke_command' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/thor-0.20.0/lib/thor.rb:387:in dispatch' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.1.6/lib/rails/command/base.rb:63:inperform' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.1.6/lib/rails/command.rb:44:in invoke' c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.1.6/lib/rails/commands.rb:16:in' bin/rails:4:in require' bin/rails:4:in' Bundler Error Backtrace: from c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:81:in block (2 levels) in require' from c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:77:ineach' from c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:77:in block in require' from c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:66:ineach' from c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.15.3/lib/bundler/runtime.rb:66:in require' from c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/bundler-1.15.3/lib/bundler.rb:108:inrequire' from c:/Sites/myrubyblog/config/application.rb:7:in <top (required)>' from c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:133:inrequire' from c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:133:in block in perform' from c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:130:intap' from c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:130:in perform' from c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/thor-0.20.0/lib/thor/command.rb:27:inrun' from c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/thor-0.20.0/lib/thor/invocation.rb:126:in invoke_command' from c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/thor-0.20.0/lib/thor.rb:387:indispatch' from c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.1.6/lib/rails/command/base.rb:63:in perform' from c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.1.6/lib/rails/command.rb:44:ininvoke' from c:/RailsInstaller/Ruby2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.1.6/lib/rails/commands.rb:16:in <top (required)>' from bin/rails:4:inrequire' from bin/rails:4:in `'

jeudi 17 mai 2018

Rails logger not release log file even after delete log file it keep taking space on disk

I have multi-threaded rails application and it has many workers which pull a message from resque queue and logs messages. I have put linux logrotate to rotate log and after log rotate it upload to S3.

but after uploaded and delete file I see disk space still used not release
when I execute below command i see lots of files.

lsof | grep deleted
ruby      14530           fadmin    7w      REG              202,1 1972144092  407325 /home/fadmin/www/fasterap/shared/log/production_database.log.1 (deleted)
ruby-time 14536 20352     fadmin    7w      REG              202,1 1972144092  407325 /home/log/production_database.log.1 (deleted)

it full diskspace every time it release space only if I kill all ruby process.

can I know what best way to rotate logs and how should I avoid the problem?

why the p returns always last variable's value

a=10 b=20 p (a && b)

The above p statement always prints the last variable value. why it is not printing both variable values.

mercredi 16 mai 2018

Divs align into each other

I am using here 2 times: col-md-6 to seperate 2 rows. It works but when I have in the first (col-md-6) more than 2 divs, it will override the other col-md-6. I am using the display: inline-flex on both col-md-6 which is probably the case why it is doing this. But I don't know how to solve it. divs into each each other

I want to have something like this but instead of the buttons under each other, next to each other. buttons under each other

Here is the code how I am doing this it is writting in ruby/haml

.container
      .row
        - my_supervisors = current_user.time_keeper_supervisors(@time_keeper)
        - if my_supervisors.count > 1 || my_supervisors.exclude?(@employee)
          .col-md-6
            = panel('Tijdlijnen mijn medewerkers') do
              - my_supervisors.sort_by(&:last_name).each do |supervisor|
                .col-sm-6.col-md-5.col-lg-3.item.icons
                  %a{href: timelines_time_keeper_employee_path(@time_keeper, supervisor.code)}
                    .box.boxAccounts
                      .boxInsiteAccount
                        %i.fa.fa-users.iconSpaceBottom.accountIcon.onlyTextColor
                        %h3.name
                          - if true || can?(:timelines, supervisor)
                            = "Medewerkers #{supervisor.name}"


        - if @employee.subordinates.supervisor.any?
          .col-md-6
            = panel('Tijdlijnen onderliggende medewerkers') do
              - @employee.subordinates.supervisor.sort_by(&:last_name).each do |supervisor|
                .col-sm-6.col-md-5.col-lg-3.item.icons
                  %a{href: timelines_time_keeper_employee_path(@time_keeper, supervisor.code)}
                    .box.boxAccounts
                      .boxInsiteAccount
                        %i.fa.fa-users.iconSpaceBottom.accountIcon.onlyTextColor
                        %h3.name
                          - if true || can?(:timelines, supervisor)
                            = "Medewerkers #{supervisor.name}"

How to implement Parent Categories, Categories and Subcategories using self referencing controller in Rails 4

I am trying to implement a three level deep association using self referencing.

Cat1
    Sub1
        SubSub1
        SubSub2
    Sub2
Cat2
    Sub1
Cat3
    Sub1
    Sub2
        SubSub1

I am able to get the child category of a category by this relation:

class Category < ActiveRecord::Base
  has_many :sub_categories, class_name: "Category", foreign_key: :parent_id
end

This is fine when i have only two level deep category. For three level deep association using self referencing i tried using this relation, but failed to get the desired output.

class Category < ActiveRecord::Base
  belongs_to :parent_category, class_name: "Category"
  has_many :sub_categories, class_name: "Category", foreign_key: :parent_id
end

here is what i get using this association.query fired on Category.find(3).parent_category is wrong.

2.0.0-p648 :012 > Category.find(2)
  Category Load (1.2ms)  SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 2 LIMIT 1
 => #<Category id: 2, title: "Suit", description: "sffdsfsxcx ssdfvvs", seo_name: "sfsdf", parent_id: nil, hoe_page: nil, status: true, sequence: "1", banner_image_file_name: nil, banner_image_content_type: nil, banner_image_file_size: nil, banner_image_updated_at: nil, image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil, home_description: "adhkadaa", home_page: true, long_description: "sdfsddfffssssde", created_at: "2018-04-09 07:42:55", updated_at: "2018-04-09 07:42:55"> 
2.0.0-p648 :013 > Category.find(3)
  Category Load (1.1ms)  SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 3 LIMIT 1
 => #<Category id: 3, title: "a", description: "aaa", seo_name: "a", parent_id: 2, hoe_page: nil, status: true, sequence: "1", banner_image_file_name: nil, banner_image_content_type: nil, banner_image_file_size: nil, banner_image_updated_at: nil, image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil, home_description: "aaa", home_page: true, long_description: "aaa", created_at: "2018-04-09 09:44:11", updated_at: "2018-04-09 09:44:11"> 
2.0.0-p648 :014 > Category.find(3).parent_category
  Category Load (1.1ms)  SELECT `categories`.* FROM `categories` WHERE `categories`.`id` = 3 LIMIT 1
 => nil 

Please help me here by making me understand what would be the perfect association for my purpose. Please dont give me gem name like "Ancestry" or "awesome_nested_set", I need pure rails associations.

dimanche 13 mai 2018

2 similar tables with an indexed category_id - 2 different queries generated by Postgres

I'm using: Rails 3.2.13 and PostgreSQL 9.4.7

I have 2 product tables, AProducts and BProducts. Both tables have a category_id integer which is an indexed column, and both tables have several million records.

When I query AProducts by category_id, the response is quick and uses the index, but when I query BProducts the response is very very slow, and it uses a sequential scan instead of using the index. I'm trying to figure out why this is happening for BProducts, given that the table and model is nearly idential to AProducts. Here are the results from EXPLAIN:

irb(main):2290:0> BProduct.where(:category_id => 700).explain

  BProduct Load (219696.7ms)  SELECT "b_products".* FROM "b_products" WHERE "b_products"."category_id" = 700
  EXPLAIN (1.3ms)  EXPLAIN SELECT "b_products".* FROM "b_products" WHERE "b_products"."category_id" = 700
=> "EXPLAIN for: SELECT \"b_products\".* FROM \"b_products\"  WHERE \"b_products\".\"category_id\" = 700\n                                 QUERY PLAN\n-----------------------------------------------------------------------------\n Seq Scan on b_products  (cost=0.00..1791663.25 rows=926 width=2036)\n   Filter: (category_id = 700)\n(2 rows)\n"

The slow query above takes 219696.7ms, and doesn't use the index. The query below for AProduct is quick and DOES use the index.

irb(main):2289:0> AProduct.where(:category_id => 700).explain
  AProduct Load (51.0ms)  SELECT "a_products".* FROM "a_products" WHERE "a_products"."category_id" = 700
  EXPLAIN (1.2ms)  EXPLAIN SELECT "a_products".* FROM "a_products" WHERE "a_products"."category_id" = 700
=> "EXPLAIN for: SELECT \"a_products\".* FROM \"a_products\"  WHERE \"a_products\".\"category_id\" = 700\n                                                           QUERY PLAN\n--------------------------------------------------------------------------------------------------------------------------------\n Index Scan using index_a_products_on_category_id on a_products  (cost=0.56..5448.33 rows=1359 width=883)\n   Index Cond: (category_id = 700)\n(2 rows)\n"

What I'm trying to figure out, is how can I make my BProduct lookups use the index and fetch the records with the same speed as AProduct, and why isn't that the default behavior I'm seeing?

mercredi 9 mai 2018

What to use: Class or Module, in ROR

I am new to ROR and trying to convert my PhP application to ROR application. There is one php file common.php which is included in almost all other files. Likewise common.php in turn uses RPCClient.php and User.php . Variables and methods are shared across php files. So, what should I use in ROR a class or a module as data is shared among various files.

Any kind of help is much appreciated, Warm Regards.

lundi 7 mai 2018

How to inject current_user with params coming from AWS Cognito?

I have recently decided to move my user base and authentication to AWS Cognito from Device. Reson for this is, my team is developing mobile apps and rails is the backend API. I have created a custom strategy ( AWS Cognito) and configured on the device to use my custom strategy. My authentication is working as expected and I am getting the user-specific attributes from the AWS Cognito. But, I do not how to populate my attributes into the device current_user object. For simplicity and maintaining the existing relations among my entities, I still maintaining local users table with only minimum information such as username, role and of course the primary key.

Can someone help me, how to populate the current_user with cognito attributes?

Following is my pseudo code for warden custom strategy:

module Devise module Strategies class CognitoAuthenticatable < Authenticatable def authenticate! client = get_aws_cognito if resp = client.initiate_auth(username,pwd) user_attrs_from_aws_cognito = resp.get_user(resp.access_token) ### How to populate user_attrs_from_aws_cognito to current user success! else fail!("In valid login") end end end end end

Rails 3: I have controller name and action and need to get path and http verb for it

In rails 3.2 I need to get path and http verb of rails endpoint by controller name and action name.

samedi 5 mai 2018

Multiple image Uploader using carrierwave for single model

I have created a gallery module and i have added two uploaders for single model. The code works fine but the when i upload the multiple images and try to edit that I get multiple options for uploading the same image. How do I fix that? Can we use two uploaders for same model or do we have some other approach do this?

model code:

class Image < ApplicationRecord
 belongs_to :gallery 
 mount_uploader :image, ImageUploader
 mount_uploader :avatar, AvatarUploader
end

class Gallery < ApplicationRecord
  has_many :images
  accepts_nested_attributes_for :images
end

class GalleriesController < AdminController   
 def index
   @galleries = Gallery.all
 end

 def show
   @images = @gallery.images.all
 end

 def new
   @gallery = Gallery.new
   @image = @gallery.images.build
 end 

 def create
   @gallery = Gallery.new(gallery_params)
   respond_to do |format|
   if @gallery.save
    params[:images]['avatar'].each do |a|
      @gallery.images.create!(:avatar => a, :gallery_id =>  @gallery.id)
    end
    params[:images]['image'].each do |a|
    @images = @gallery.images.create!(:image => a, :gallery_id =>  @gallery.id)
   end
   format.html { redirect_to @gallery, notice: 'Gallery was successfully created.' }
   format.json { render :show, status: :created, location: @gallery }
 else
   format.html { render :new }
   format.json { render json: @gallery.errors, status: :unprocessable_entity }
 end
 end
end

 def gallery_params
  params.require(:gallery).permit(:title, :details, :status, images_attributes:[:id, :gallery_id, :image, :avatar])
 end   

Image Controller code:

class ImagesController < AdminController
  def image_params
   params.require(:image).permit(:gallery_id, :slideshow_id,:image, :avatar)
  end
 end
[![edit page ][1]][1]
form page:
 <%= form.fields_for :images do |p| %>
  <div class="field">
   <%= p.label :master_image, class: "col-2 col-form-label" %>
  <%= p.file_field :avatar, :multiple => true, name: "images[avatar][]" %>
 </div>
<% end %> 
<%= form.fields_for :images do |p| %>
 <div class="field">
   <%= p.label :image, class: "col-2 col-form-label" %>
   <%= p.file_field :image, :multiple => true, name: "images[image][]" %>
 </div>
<% end %>

vendredi 4 mai 2018

Port Scanning Python

python

**When using the response=subprocess.Popen([],stdout=).stdout.read(), what is stored in response if we don’t use .stdout.read()? **

Nokogiri put tags with separation

I'd like to message separate with :

teste = Nokogiri::XML::DocumentFragment.parse("")
    Nokogiri::XML::Builder.with( teste ){ |x|  
        x.exemplo "teste xml"
    }
    puts teste.to_xml

Message printing

<exemplo>teste xml</exemplo>

Message you want

teste

jeudi 3 mai 2018

Rails eager load custom has_many on index

Ruby 2.1, Rails 3.2
I've got this relationship in my project model

scope :active, -> { where( deleted_at: nil ) }
has_many :foremen, class_name: "ProjectsUser",  conditions: ['current_foreman = 1']

in the controller:

@projects = Project.includes(:foremen).active

which generates this SQL

ProjectsUser Load (3.3ms)  SELECT `projects_users`.* FROM `projects_users` WHERE `projects_users`.`project_id` IN (122, 130, ...etc.) AND ( current_foreman = 1)

but when i call the relationship in the view...

<% @projects.each do |project| %>
  <%= project.foremen %>
<% end %> 

it runs another SQL query each time.

ProjectsUser Load (2.5ms)  SELECT `projects_users`.* FROM `projects_users` WHERE `projects_users`.`project_id` = 122 AND (current_foreman = 1 )
ProjectsUser Load (2.5ms)  SELECT `projects_users`.* FROM `projects_users` WHERE `projects_users`.`project_id` = 130 AND (current_foreman = 1 )
ProjectsUser Load (2.5ms)  SELECT `projects_users`.* FROM `projects_users` WHERE `projects_users`.`project_id` = 151 AND (current_foreman = 1 )
etc...

is am i misunderstanding something? shouldn't these records already be loaded?
how do I write this so it only runs one query for the ProjectsUser?

How can I use google or-tools in ruby for my rails app?

I need to solve tsp problem in my rails application. I want to use google or-tools for solving this problem of tsp. The documentation1 tells about using or-tools in c++, c#, java and python. The code for solving tsp using or-tools in python is working fine. But I need to use or-tools in ruby for my rails app. How can I do this?

mercredi 2 mai 2018

check if bson array include string id

I would like to know if an array of bsons include a string id, something like this:

[186] pry(main)> changed = [BSON::ObjectId('5ae9972ea8abbc3c5484cd5e')] 
=> [BSON::ObjectId('5ae9972ea8abbc3c5484cd5e')]
[187] pry(main)> changed
=> [BSON::ObjectId('5ae9972ea8abbc3c5484cd5e')]
[188] pry(main)> manager_to_remove = "5ae9972ea8abbc3c5484cd5e"
=> "5ae9972ea8abbc3c5484cd5e"
[189] pry(main)> manager_to_remove
=> "5ae9972ea8abbc3c5484cd5e"
[190] pry(main)> changed.include?(manager_to_remove).inspect
=> "false"

I'm always getting false

Thank you!

mardi 1 mai 2018

Rails AR Date Range Query - Daylight Saving - Overlap

I have a Rails 3 application, how would one avoid an overlap happening due to daylight saving?

My problem is that I am having a form that generate reports. Auditing an inconsistency I noticed that a bunch of transactions show up in the week ending in March 11th also show up in the Week starting on March 12th.

The problem boils down to some thing like this...

Time.zone.parse('2018-03-11').to_datetime.end_of_day.utc
 => Mon, 12 Mar 2018 07:59:59 +0000 
Time.zone.parse('2018-03-12').to_datetime.beginning_of_day.utc
 => Mon, 12 Mar 2018 07:00:00 +0000 

The 1 hour overlap above seem to be where my problem lies. When checking date ranges (see actual code below) how can I avoid this overlap.

Actual Code

Here is the actual code that resemble filtering by date.

  scope :filter_date, lambda { |starts, ends, date, transaction_type = :transaction|
    _scope = scoped


    starts = Time.zone.parse(starts).to_datetime        if starts.class == String and starts.present?
    ends   = Time.zone.parse(ends).to_datetime.tomorrow if ends.class   == String and ends.present?

    begin
      case date
      when 'settled'
        transaction_type == "batch" ? date_field = 'deposited_at' : date_field = 'settled_at'
        _scope = _scope.order('transactions.'+date_field+' DESC')
        _scope = _scope.where("transactions."+date_field+" >= ?", starts) if starts.present?
        _scope = _scope.where("transactions."+date_field+" < ?", ends)   if ends.present?
      else # created, nil, other
        _scope = _scope.order('transactions.created_at DESC')
        _scope = _scope.where("transactions.created_at >= ?", starts) if starts.present?
        _scope = _scope.where("transactions.created_at < ?", ends)   if ends.present?
      end
    end
    _scope
  }

Stack

  • Ruby 2.1
  • Rails 3.2
  • PG

Question

How can I overcome this overlap of time where the daylight saving takes effect.