mercredi 10 mars 2021

Setting up database for the first time in ruby on rails

I am very much fresh in using Ruby on rails. I have cloned a git repo and trying to set up a PostgreSQL database in my local machine(Win 10).

Ruby version- 2.4.10
Rails - 5.2.0

I am using the below command

rake db:setup RAILS_ENV=development

Error Output -

rake aborted!
NoMethodError: undefined method `[]' for nil:NilClass
D:/ruby/reserves/config/environments/development.rb:41:in `block in <top (required)>'
D:/ruby/reserves/config/environments/development.rb:1:in `<top (required)>'
D:/ruby/reserves/config/environment.rb:5:in `<top (required)>'
Tasks: TOP => db:setup => db:schema:load_if_ruby => db:create => db:load_config => environment
(See full trace by running task with --trace) 

In database.yml file there are three environments has been declared - Development/Production/test

development:
  <<: *default
  database: reserves-dev
  username:****
  password:****
  host: localhost

How can I set the environment variable to development for setting the database in my local.

Any help is highly appreciated.

Thank you in advance.

lundi 8 mars 2021

What is the best gem for managing con jobs?

I want to write a cron job in order to shoot a notification about an event happening soon. I want to notify the participants about the event one day before it's happening day. I want to call a controller method which will check event start date and if it is happening tomorrow it should be called automatically and send notifications to the participants.I tried whenever gem but did not work for me or might be I don't know how to use it for my case. Please suggest me a good gem which will fit in my use case. Thanks.

samedi 6 mars 2021

After changing my Rails Version to 6.0.3.5 from 6.1.3 for Heroku. I have got a Runtime Error after running Rails S

enter image description here

When deploying basic app for the first time to Heroku I had to change my Rails Version to 6.0.3.5 from 6.1.3 because Heroku wasn't working on Rails 6.1.3. After changing the version to 6.0.3.5 in the Gemfile and running "bundle" and "bundle update". I am getting a Runtime error as attached. I am a beginner, help to right direction would be great.

vendredi 5 mars 2021

RSpec: Can we configure an expectation to return two different value? I want to test retry mechanism

So I have a retry rule in my application. I wanted to test that using rspec.

When I call service without passing account ID it should give me false and when account ID is passed it should give me true.

I am trying this code

options = {
  email: 'dev@dev.com'
}
expect_any_instance_of(PaymentService::CreatePayment).to receive(:call)
                                                           .with(options)
                                                           .and_return(false)
options[:account_id] = 12345
expect_any_instance_of(PaymentService::CreatePayment).to receive(:call)
                                                           .with(options)
                                                           .and_return(true)

But this doesnt work for me.

Rails upsert using find_or_create_by, create_with and optional chaining

I am using below code to upsertmy Model which works correctly but is verbose. I wanted to shorten it by using find_or_create_by, create_with and optional chaining but it is not working as expected.

Can someone please advice...

Verbose Code which works -

user_attr = {
              first_name: 'Scarlett',
              last_name: 'Johansson',
              profession: 'Actress',
              address: 'Los Angeles'
            }

....
existing_user = User.find_by(first_name: user_attr.fetch(:first_name), 
                             last_name: user_attr.fetch(:last_name))
if existing_user
  existing_user.update!(user_attr)
 else
  User.create!(user_attr)
end

Output:
# => #<User id: 2, first_name: "Scarlett", last_name: "Johansson", profession: "Actress", address: "Los Angeles">

Shorter version which does not return correct output:

User
  .create_with(user_attr)
  .find_or_create_by(first_name: user_attr.fetch(:first_name), 
                     last_name: user_attr.fetch(:last_name))

Reference - https://apidock.com/rails/v4.0.2/ActiveRecord/Relation/find_or_create_by

mardi 2 mars 2021

How to deep copy in ruby without using marshal?

this is what my project says :- "You must implement deep copying of lists yourself, without relying on Ruby constructs for marshalling and unmarshalling data structures" How do you do that?

How to make sure that mongoid attribute values are updated in a specific order?

I have an enum enumerize :message_status, in: [:sent, :delivered, :read, :error] in my mongoid class WhatsappLog. I want to make sure that the message status can be updated only in this particular order. "sent" -> "delivered" -> "read". For example, if the message status for a particular document is "delivered", I want to prevent it from being changed to "sent".