mardi 24 janvier 2023

Ruby on Rails performance of search engine with indexed column

Hi i'm try to check search engine's performance of my ROR application. I have 4 search input forms : title, content, created_on (date) and updated_on (date) I want to check performace of search depending on the presence or absence of an index. (in my case, index presence on created_on and absence on updated_on)

My controller of Post

  def index
    search_start_time = Time.now
    @posts = Post.search(params[:title], params[:content], params[:created_on], params[:updated_on])

    # this line for check performance of search
    puts Time.now - search_start_time
  end

My schema

  create_table 'posts', force: :cascade do |t|
    t.string 'title', null: false
    t.string 'content', null: false
    t.date 'created_on', null: false, index: true
    t.date 'updated_on', null: false
  end

In my post.rb, i maked search method like this

  def self.search(title, content, started_on, finished_on)
    where([
      "title LIKE ? AND content LIKE ? AND CAST(started_on AS text) LIKE ? AND CAST(finished_on AS text) LIKE ?",
      "%#{title}%", "%#{content}%", "%#{started_on}%", "%#{finished_on}%"
    ])
  end

With my code, i performance but there were not big difference with search performance of "indexed" and "not indexed" columns.

Is there a problem with my code? Or does the index not affect the search results? The number of records is 10 million, and an indexed column always comes out similar to the speed of an unindexed column.

I tried to change my search method like this ->

  def self.search(title = '', content = '', started_on = '', finished_on = '')

But there was not difference.

lundi 23 janvier 2023

Search users based on multiple elements

I am trying to impliment search functionality for user records with first_name, last_name, email, project_name,feature_name. Here first_name, last_name and email is from one table(User), project_name from table Project and feature_name from table Feature.Association of models are given below.I have a user index page where lists all users from table User. Need a search which search for users which we are entering.

model user.rb:

     has_many :project_users, dependent: :destroy
     has_many :projects, through: :project_users, dependent: :destroy
   end```

User have fields of first_name, last_name, email etc(using these three fields for search)

model project.rb

```class Project < ApplicationRecord
     belongs_to :user
     has_many :project_users, dependent: :destroy
     has_many :features, dependent: :destroy
     has_many :users, through: :project_users, source: :user
   end```
Project have project_name(we search using project name)

model feature.rb

```class Feature < ApplicationRecord
     belongs_to :project
   end```

Feature have feature_name(with feature_name we need to search)

**What I am look for**

We have params[:search_member] which contains the searched item(first_name, last_name, email, project_name, feature_name

For ex: params[:search_member] = "John"
        params[:search_member] = "Project1"
        params[:search_member] = "Feature1"
        params[:search_member] = "john@gmail.com"

Need a single query which checks the "params[:search_member]" in these three tables(User, Project and Feature) in fields first_name, last_name, email, project_name, and feature_name and return the users of searched value.

Working of associations

current_user.projects = will return all projects belongs to current user

project.users = return all users belongs to project

feature.project = return project that feature belongs to

and

feature.project.users will return all users of projects


```def search_all
  if params[:search_member].present?
     #need query here
  else
     User.all
  end
end```


1) If I enter project_name it will return all users of that particular project

2) If I enter first_name, last_name or email return all users of this details

3) If I enter feature name, return all users of project that the feature belongs to

Trying to do in a single joins query

Ruby on rails link_to delete method doesn't work

I used this code to delete a form in my app but it doesn't work

<td><%= link_to 'Destroy', resource, method: :delete, data: { confirm: 'Are you sure?' } %></td>

When I click it displays the information entered in the form. I already tried to solve the problem with jquery but it didn't work.

I have this in my terminal:

> Started GET "/resources/2" for 127.0.0.1 at 2023-01-23 10:43:44 +0100
Processing by ResourcesController#show as HTML
  Parameters: {"id"=>"2"}
  Resource Load (15.4ms)  SELECT "resources".* FROM "resources" WHERE "resources"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  ↳ app/controllers/resources_controller.rb:63:in `set_resource'
  User Load (5.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Rendering layout layouts/application.html.erb
  Rendering resources/show.html.erb within layouts/application
  Rendered resources/show.html.erb within layouts/application (Duration: 3.4ms | Allocations: 412)
  Rendered layout layouts/application.html.erb (Duration: 30.8ms | Allocations: 2972)
Completed 200 OK in 291ms (Views: 43.5ms | ActiveRecord: 121.3ms | Allocations: 10816)


Started GET "/resources" for 127.0.0.1 at 2023-01-23 11:05:41 +0100
Processing by ResourcesController#index as HTML
  User Load (23.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Rendering layout layouts/application.html.erb
  Rendering resources/index.html.erb within layouts/application
  Resource Load (4.6ms)  SELECT "resources".* FROM "resources"
  ↳ app/views/resources/index.html.erb:16
  Rendered resources/index.html.erb within layouts/application (Duration: 17.5ms | Allocations: 1379)
  Rendered layout layouts/application.html.erb (Duration: 46.8ms | Allocations: 3317)
Completed 200 OK in 476ms (Views: 59.7ms | ActiveRecord: 181.9ms | Allocations: 9944)

Can anyone tell me how to solve this problem?

Thank you!

jeudi 19 janvier 2023

How can I add Adminer to my Ruby on Rails application with PostgreSQL?

I am new to ruby on rails. This question may be simple. I have been trying to add an adminer folder on the app on ruby on rails application. My database is PostgreSQL. I want to manage my database on the browser using adminer.

I wish someone could help me get an answer as to whether it's possible to add admins to a Ruby on Rails app. If you can add admins, please briefly explain how to do so. Thank you in advance.

mardi 17 janvier 2023

Convert date string to Date where Day is missing

I know i can use to_date or parse to convert a string to a date in rails, but how do i convert to a date if the string is of the format "01-2018", where 01 is the month (January) and the day is missing ? Must be simple but i am having trouble doing it.

Trying it returns unexpected results, I am guessing because the day is missing:

3.0.2 :004 > Date.parse(x)  => Sun, 01 Jan 2023
3.0.2 :005 > x  => "01-2018"
3.0.2 :006 > x.to_date  => Sun, 01 Jan 2023

I guess i can prefix the string with sth like "01-", but is there a better way?

lundi 16 janvier 2023

Ruby run project while error how can ı solve this problem

Hi ı wanna run ruby project.I write rails server -e development but ı get an error

/home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/activesupport-3.2.9/lib/active_support/values/time_zone.rb:270: warning: circular argument reference - now Traceback (most recent call last): 13: from script/rails:6:in ' 12: from script/rails:6:in require' 11: from /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/railties-3.2.9/lib/rails/commands.rb:50:in <top (required)>' 10: from /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/railties-3.2.9/lib/rails/commands.rb:50:in tap' 9: from /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/railties-3.2.9/lib/rails/commands.rb:53:in block in <top (required)>' 8: from /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/railties-3.2.9/lib/rails/commands.rb:53:in require' 7: from /home/deployer/apps/isg/20220314091757/config/application.rb:15:in <top (required)>' 6: from /usr/lib/ruby/vendor_ruby/bundler.rb:114:in require' 5: from /usr/lib/ruby/vendor_ruby/bundler/runtime.rb:65:in require' 4: from /usr/lib/ruby/vendor_ruby/bundler/runtime.rb:65:in each' 3: from /usr/lib/ruby/vendor_ruby/bundler/runtime.rb:76:in block in require' 2: from /usr/lib/ruby/vendor_ruby/bundler/runtime.rb:76:in each' 1: from /usr/lib/ruby/vendor_ruby/bundler/runtime.rb:80:in block (2 levels) in require' /usr/lib/ruby/vendor_ruby/bundler/runtime.rb:84:in rescue in block (2 levels) in require': There was an error while trying to load the gem 'delayed_job_active_record'. (Bundler::GemRequireError) Gem Load Error is: undefined method yaml_as' for ActiveRecord::Base:Class Did you mean? yaml_tag Backtrace for gem load error is: /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/activerecord-3.2.9/lib/active_record/dynamic_matchers.rb:50:in method_missing' /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/delayed_job-4.0.4/lib/delayed/serialization/active_record.rb:4:in class:Base' /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/delayed_job-4.0.4/lib/delayed/serialization/active_record.rb:3:in <module:ActiveRecord>' /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/delayed_job-4.0.4/lib/delayed/serialization/active_record.rb:2:in <top (required)>' /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:251:in require' /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:251:in block in require' /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:236:in load_dependency' /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:251:in require' /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/delayed_job-4.0.4/lib/delayed/worker.rb:64:in backend=' /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/delayed_job_active_record-4.0.2/lib/delayed_job_active_record.rb:5:in <top (required)>' /usr/lib/ruby/vendor_ruby/bundler/runtime.rb:81:in require' /usr/lib/ruby/vendor_ruby/bundler/runtime.rb:81:in block (2 levels) in require' /usr/lib/ruby/vendor_ruby/bundler/runtime.rb:76:in each' /usr/lib/ruby/vendor_ruby/bundler/runtime.rb:76:in block in require' /usr/lib/ruby/vendor_ruby/bundler/runtime.rb:65:in each' /usr/lib/ruby/vendor_ruby/bundler/runtime.rb:65:in require' /usr/lib/ruby/vendor_ruby/bundler.rb:114:in require' /home/deployer/apps/isg/20220314091757/config/application.rb:15:in <top (required)>' /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/railties-3.2.9/lib/rails/commands.rb:53:in require' /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/railties-3.2.9/lib/rails/commands.rb:53:in block in <top (required)>' /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/railties-3.2.9/lib/rails/commands.rb:50:in tap' /home/deployer/apps/isg/20220314091757/vendor/bundle/ruby/2.5.0/gems/railties-3.2.9/lib/rails/commands.rb:50:in <top (required)>' script/rails:6:in require' script/rails:6:in ' Bundler Error Backtrace:`

I install node js but doesnt work

Got the error File type is not supported when uploading a file in ruby on rails

 url = URI("https://api.podium.com/v4/messages/attachment")
      https = Net::HTTP.new(url.host, url.port)
      https.use_ssl = true
      request = Net::HTTP::Post.new(url)
      request["Content-Type"] = "multipart/form-data"
      request["Authorization"] = "Bearer #{access_token}"
      form_data = [["attachment",File.open('D:\proj\v5\ap\fl\Screenshot (1).png')],['data', "#{request_data}"]]
      request.set_form(form_data, 'multipart/form-data')
      response = https.request(request)
      response_body = JSON.parse(response.body)
      if response.code == '200' || response.code == '201'
          return response_body,'success'
      else
           return response_body,"#{response.message}"
      end
      rescue Exception => ex
        return ex,'Exception'
     end

** When i am sending the request i got the error like

{"code"=>"invalid_request_values", "message"=>"File type is not supported.", "moreInfo"=>"https://ift.tt/ahE7HgF} **

jeudi 12 janvier 2023

ActiveRecord::StatementInvalid in Summaries#index

PG::UndefinedColumn: ERROR: column summaries.user does not exist LINE 1: SELECT "summaries".* FROM "summaries" WHERE "summaries"."use...

I'm trying to make a view restriction, where only the admin can see all the records in an app

TypeError: superclass must be a Class (Module given)

I have a class which inherits another class

Class A
end

Class A::B < A
end

I am trying to write specs for class B by describing it as:

describe A::B, type: :class do
 #some test here
end

But i get this error: TypeError: superclass must be a Class (Module given) on spec file'd describe line. How can i fix this?

vendredi 6 janvier 2023

Can i use variables in Rails link_to method?

Can i use variable for Rails link_to helper for making different link with variables?

For example, <%= link_to users, users_path %> I have link like this,

And i'd like to change this url with variable examples

So i changed url like this, <%= link_to users, "#{examples}_path" %> This not worked because of whole string change to url.

How can i change my link_to for use this with different variable for DRY codes?

mercredi 4 janvier 2023

omniauth: (linkedin) Authentication failure! invalid_credentials

Request phase initiated. Started GET "/users/auth/linkedin/callback?code=...&state=..." for 127.0.0.1 at 2022-09-14 20:50:20 +0200 D, [2022-09-14T20:50:20.537126 #37240] DEBUG -- omniauth: (linkedin) Callback phase initiated. E, [2022-09-14T20:50:21.021252 #37240] ERROR -- omniauth: (linkedin) Authentication failure! invalid_credentials: OAuth2::Error, invalid_request: A required parameter "client_secret" is missing {"error":"invalid_request","error_description":"A required parameter \"client_secret\" is missing"} Processing by Users::OmniauthCallbacksController#failure as HTML

ruby version 3.2.0 rails version 7.0.4

gem 'omniauth' gem 'omniauth-linkedin-oauth2'