How to extract values from an array

How can I extract a particular number from the below array values between two underscores names = [ "create_sales_3920_7873","create_sales_49204_7873","create_sales_392_7873"] How to get output result as 3920, 49204, 392 from the above array Any help is appreciable >Solution : If the actual names array is quite long I’m sure there’s a better way to do this,… Read More How to extract values from an array

Call a static superclass method from inside a subclass method

Hi I am new to Ruby and I don’t understand why the call to a superclass’ static method doesn’t work with the super keyword, I get this kind of error super: no superclass method `creer_adherent’ for Adherent:Class This is the superclass : class Personne … def self.create(type) print "Nom:" nom = gets.chomp print "PrĂ©nom:" prenom… Read More Call a static superclass method from inside a subclass method

How can assign value on rails model

I am trying to set the value in the User model on the after_create callback, but one of my model field values is not getting set. User.find_or_create_by!(company_id: 1) Model before_create: set_value def set_value self.assign_attributes( company_name: self.company.name location: self.company.location ) end Location values are set in the user model, but company_name is always nil. I don’t… Read More How can assign value on rails model

Convert date format in Serializer

I am displaying my data like this.. Can someone suggest a way to display the last date as February 02,2023 using serializer? "category": { "data": { "id": "81", "attributes": { "id": 81, "name": "ggg", "last_date": "2023-02-22", "created_at": "2023-02-15T13:01:53.341+05:30", "updated_at": "2023-02-15T13:01:53.341+05:30", } } } I have tried adding below in my serializer.. but it is not… Read More Convert date format in Serializer

How can I idiomatically write a doubly nested if/else in Ruby?

Suppose I have some code like: def set_reminder(cond_one: false, cond_two: false) if cond_two if cond_one outcome_a else outcome_b end else if cond_one outcome_c else outcome_d end end end How can I more elegantly write a function like this, which has 4 potential results (one for each combination of possible cond_one and cond_two values)? I’m not… Read More How can I idiomatically write a doubly nested if/else in Ruby?

Correct syntax to call a mailer with interpolation

I have a method to dynamically call a mailer that look like this : def mail application = mail[:application].capitalize class_template = mail[:template].split(‘/’).[0]+ ‘Mailer’ template = mail[:template].split(‘/’).second email_params = mail[:params].values.map(&:inspect).join(‘, ‘) "#{application}::#{class_template}.#{template}(#{email_params}).deliver_now" # Here I have something like : "Application::TemplatetMailer.test_name(\"John\", \"Doe\").deliver_now" end How can I have something like : Application::TemplatetMailer.test(\"John\", \"Doe\").deliver_now instead of "Application::TemplatetMailer.test(\"John\", \"Doe\").deliver_now" >Solution… Read More Correct syntax to call a mailer with interpolation

appending the outputs of an array method into another array

array3 =[‘Maths’, ‘Programming’, ‘Physics’], [‘Maths’, ‘Intro to comp. science’, ‘Programming’], [‘English’, ‘Intro to comp. science’, ‘Physics’] course = ‘Programming’ index = [] array3.find_index do |i| if array3.include?(course) == true index << i end end i created an array (array3) that contains the respective elements and i want to add the elements of array3 which hold… Read More appending the outputs of an array method into another array