jeudi 6 juillet 2017

String value gets converted to Time object when a model method is called using delayed job

I am using delayed job to queue a Model method in another Model like this:

article_loader.rb

date_value_in_string =  "2017-06-21 07:17:00"
Article.delay(:queue => 'article_load').article_loading([date_value_in_string])

Even though I have passed a String as an argument to the method, inside the method it gets converted to Time object.

article.rb

def self.article_loading(args)
  date_value = args[0]
  p date_value.class # Time
end

I don't know why this happens. Any help will be appreciated.

mercredi 5 juillet 2017

Mysql server cannot connect through mysql socket (2) when app is running in vagrant Ubuntu VM

I have a rails app which was perfectly working for me in my default rails development environment, but when I installed Vagrant VM and ran my app on Ubuntu 12.04 I have this error that says

Mysql2::Error (Can't connect to local MySQL server through socket 'MySQL' (2)):

As far as I understood is that my ubuntu VM cannot load Mysql server through socket MYSQL that was working for my default windows, so I tried to remove socket: MySQL line from my rails database.yml

default: &default


adapter: mysql2
  encoding: utf8
  pool: 5
  username: root
  password: xxxxx
  socket: MySQL
  host: localhost

But then I get the error saying my database is not found, I can't seem to fig out how can I look for my database from window and load it in Ubuntu VM ??

here is the log that gives absolutely no info .. :/

Mysql2::Error (Can't connect to local MySQL server through socket 'MySQL' (2)):

/home.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/mysql2-0.4.5/lib/mysql2/client.rb:89:in `connect'
/home.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/mysql2-0.4.5/lib/mysql2/client.rb:89:in `initialize'
/home.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/activerecord-5.0.1/lib/active_record/connection_adapters/mysql2_adapter.rb:25:in `new'
/home.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/ ...so on

Any Ideas on how to make it work ?

mardi 4 juillet 2017

Get parent objects without active childs

I've two models, Doctor and Clinic where a doctor has_many clinics.

Clinic have doctor_id and a boolean active field.

What I want:

I want to get all doctors which does not have any active (active field is true) clinics i.e. either doctor does not have any clinics or does not have any clinics where active field is true.

What I've tried so far:

Try 1:

scope :incomplete_doctors, -> { includes(:clinics)
                                .where("( doctor_clinics.id IS NULL ) OR
                                        ( doctor_clinics.id IS NOT NULL AND
                                            doctor_clinics.active=?)", false )
                              }

Try 2:

scope :incomplete_doctors, -> { where("id NOT IN (?)", self.includes(:clinics)
                            .where("( doctor_clinics.doctor_id IS NULL ) OR
                                      ( doctor_clinics.doctor_id IS NOT NULL AND
                                          doctor_clinics.active=?)", false )
                            .select(:id))
                            }

Try 3:

SELECT "doctors".* FROM "doctors"
  LEFT OUTER JOIN "doctor_clinics" ON "doctor_clinics"."doctor_id" = "doctors"."id"
  WHERE ( ( doctor_clinics.id IS NULL ) OR
          ( doctor_clinics.id IS NOT NULL AND
              doctor_clinics.active='f'))
  GROUP BY doctors.id
    HAVING 'true' <> ANY(array_agg(DISTINCT doctor_clinics.active::TEXT));

Success:

I'm able to achieve desired output using following method, but I want to achieve this using a SQL query.

def active_clinics
  clinics.active_clinics # active_clinics is a scope in Clinic model while give all active clinics
end

def self.incomplete_doctors
  (Doctor.all.map { |d| d unless d.active_clinics.present? }).compact
end

Recaptcha::RecaptchaError in Articles#show No site key specified

Help please. I get:

"Recaptcha::RecaptchaError in Articles#show No site key specified."

And I do not understand where my mistake is. :( Please tell me how to protect comments?

http://ift.tt/2tmJ9eW

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

i have created new rails app and try to run bundle install it shows this error,

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
current directory: /var/lib/gems/2.3.0/gems/json-1.8.6/ext/json/ext/generator/usr/bin/ruby2.3 -r ./siteconf20170704-11395-12tpg5u.rb extconf.rb
mkmf.rb can't find header files for ruby at /usr/lib/ruby/include/ruby.h
extconf failed, exit code 1

Gem files will remain installed in /var/lib/gems/2.3.0/gems/json-1.8.6 for inspection.
Results logged to /var/lib/gems/2.3.0/extensions/x86_64-linux/2.3.0/json-1.8.6/gem_make.out

An error occurred while installing json (1.8.6), and Bundler cannot continue.
Make sure that `gem install json -v '1.8.6'` succeeds before bundling

Kindly send me some suggestions,Thanks in Advance.

lundi 3 juillet 2017

Model serilize hash turned from hash to string when saving object

ruby 2.3.3, rails 3.2.22.5

Hi. I'm using a serialized :logs, Hash in a User model, when saving the logs hash- afterwards it's being retrieved as string instead of Hash.

How it happen:

  1. assign value to logs hash
  2. save user object
  3. now logs hash is a string..

So for user when assigning value to logs hash: user.logs[2] = 4, then saving

class User < ActiveRecord::Base
  serialize :logs, Hash # in database it's a text type
end

An example in the rails console:

# rails console
test_user_id = 3
user = User.find(test_user_id)
user.logs = {}
user.save

user.logs # => {} # great
User.find(test_user_id).logs # => {} # great
# now trying to add value and save:

# 1. assign value to logs hash
user.logs[Time.now] = {:field_id=>6} # => {:field_id=>6}
# 2. save user object:
user.save
# 3. now logs hash is a string..
user.logs
#=> "---\n2017-07-03 19:33:34.938364000 +03:00:\n  :field_id: 6\n" 
# why it turned to string ?!?!

User.find(test_user_id).logs # same here:
# => "---\n2017-07-03 19:33:34.938364000 +03:00:\n  :field_id: 6\n"

Any ideas?

dimanche 2 juillet 2017

Ruby on rails devise form helper

I'm starting with ruby and rails and I want to create a helper to show this form :

<%= form_for( :user, :url => session_path( :user) ) do |f| %>
<%= f.text_field :email %>
<%= f.password_field :password %>
<%= f.check_box :remember_me %>
<%= f.label :remember_me %>
<%= f.submit 'Sign in' %>
<%= link_to "Forgot your password?", new_password_path( :user) %>
<% end %>

So I just have to call my function show_login-form and display my form :) but I don't know how :(

Thanks