dimanche 5 juin 2022

Ruby - PG::UndefinedTable: ERROR: missing FROM-clause entry for table

I'm getting the following error when I'm trying to query another table in a model definition.

PG::UndefinedTable: ERROR: missing FROM-clause entry for table "miq_user_roles" LINE 1: ..." = $1 AND "service_templates"."display" = $2 AND "miq_user_... ^ [catalog/explorer]

Any idea how to resolve this?

  scope :with_service_template_catalog_id,          ->(cat_id) { where(:service_template_catalog_id => cat_id) }
  scope :without_service_template_catalog_id,       ->         { where(:service_template_catalog_id => nil) }
  scope :with_existent_service_template_catalog_id, ->         { where.not(:service_template_catalog_id => nil) }
  scope :displayed,                                 ->         { where(:display => true) }
  scope :public_service_templates,                  ->         { where("miq_user_roles.settings" => nil) }

Here is the full code:

class ServiceTemplate < ApplicationRecord
  include SupportsFeatureMixin

  DEFAULT_PROCESS_DELAY_BETWEEN_GROUPS = 120

  GENERIC_ITEM_SUBTYPES = {
    "custom"          => N_("Custom"),
    "vm"              => N_("Virtual Machine"),
    "playbook"        => N_("Playbook"),
    "hosted_database" => N_("Hosted Database"),
    "load_balancer"   => N_("Load Balancer"),
    "storage"         => N_("Storage")
  }.freeze

  SERVICE_TYPE_ATOMIC    = 'atomic'.freeze
  SERVICE_TYPE_COMPOSITE = 'composite'.freeze

  RESOURCE_ACTION_UPDATE_ATTRS = [:dialog,
                                  :dialog_id,
                                  :fqname,
                                  :configuration_template,
                                  :configuration_template_id,
                                  :configuration_template_type].freeze

  include CustomActionsMixin
  include ServiceMixin
  include OwnershipMixin
  include NewWithTypeStiMixin
  include TenancyMixin
  include ArchivedMixin
  include CiFeatureMixin
  include_concern 'Filter'
  include_concern 'Copy'

  validates :name, :presence => true
  belongs_to :tenant

  has_many   :service_templates, :through => :service_resources, :source => :resource, :source_type => 'ServiceTemplate'
  has_many   :services

  has_many :service_template_tenants, :dependent => :destroy
  has_many :additional_tenants, :through => :service_template_tenants, :source => :tenant, :dependent => :destroy

  has_one :picture, :dependent => :destroy, :as => :resource, :autosave => true

  belongs_to :service_template_catalog
  belongs_to :zone
  belongs_to :currency, :inverse_of => false

  has_many   :dialogs, -> { distinct }, :through => :resource_actions
  has_many   :miq_schedules, :as => :resource, :dependent => :destroy

  has_many   :miq_requests, :as => :source, :dependent => :nullify
  has_many   :active_requests, -> { where(:request_state => MiqRequest::ACTIVE_STATES) }, :as => :source, :class_name => "MiqRequest"

  virtual_column   :type_display,                 :type => :string
  virtual_column   :template_valid,               :type => :boolean
  virtual_column   :template_valid_error_message, :type => :string
  virtual_column   :archived,                     :type => :boolean
  virtual_column   :active,                       :type => :boolean

  default_value_for :internal, false
  default_value_for :service_type, SERVICE_TYPE_ATOMIC
  default_value_for(:generic_subtype) { |st| 'custom' if st.prov_type == 'generic' }

  virtual_has_one :config_info, :class_name => "Hash"


  scope :with_service_template_catalog_id,          ->(cat_id) { where(:service_template_catalog_id => cat_id) }
  scope :without_service_template_catalog_id,       ->         { where(:service_template_catalog_id => nil) }
  scope :with_existent_service_template_catalog_id, ->         { where.not(:service_template_catalog_id => nil) }
  scope :displayed,                                 ->         { where(:display => true) }
  scope :public_service_templates,                  ->         { where("miq_user_roles.settings" => nil) }

Ruby - Query another table in a model definition

I want to query another table in a model definition. For instance, I have a table called miq_user_roles and I want to query and retrievesettings column value.

I tried adding the following

has_many   :miq_user_roles

but when I try the where condition where(:settings => nil)

I get the error service_template doesn't have settings column. How can I query miq_user_roles for settings instead of service_template

service_template has a column called miq_group_id and its the id of miq_user_rolestable.

Following is the actual model definition where I want to include miq_user_roles table.

class ServiceTemplate < ApplicationRecord
  include SupportsFeatureMixin

  DEFAULT_PROCESS_DELAY_BETWEEN_GROUPS = 120

  GENERIC_ITEM_SUBTYPES = {
    "custom"          => N_("Custom"),
    "vm"              => N_("Virtual Machine"),
    "playbook"        => N_("Playbook"),
    "hosted_database" => N_("Hosted Database"),
    "load_balancer"   => N_("Load Balancer"),
    "storage"         => N_("Storage")
  }.freeze

  SERVICE_TYPE_ATOMIC    = 'atomic'.freeze
  SERVICE_TYPE_COMPOSITE = 'composite'.freeze

  RESOURCE_ACTION_UPDATE_ATTRS = [:dialog,
                                  :dialog_id,
                                  :fqname,
                                  :configuration_template,
                                  :configuration_template_id,
                                  :configuration_template_type].freeze

  include CustomActionsMixin
  include ServiceMixin
  include OwnershipMixin
  include NewWithTypeStiMixin
  include TenancyMixin
  include ArchivedMixin
  include CiFeatureMixin
  include_concern 'Filter'
  include_concern 'Copy'


  validates :name, :presence => true
  belongs_to :tenant

  has_many   :service_templates, :through => :service_resources, :source => :resource, :source_type => 'ServiceTemplate'
  has_many   :services


  has_many :service_template_tenants, :dependent => :destroy
  has_many :additional_tenants, :through => :service_template_tenants, :source => :tenant, :dependent => :destroy

  has_one :picture, :dependent => :destroy, :as => :resource, :autosave => true

  belongs_to :service_template_catalog
  belongs_to :zone
  belongs_to :currency, :inverse_of => false

  has_many   :dialogs, -> { distinct }, :through => :resource_actions
  has_many   :miq_schedules, :as => :resource, :dependent => :destroy

  has_many   :miq_requests, :as => :source, :dependent => :nullify
  has_many   :active_requests, -> { where(:request_state => MiqRequest::ACTIVE_STATES) }, :as => :source, :class_name => "MiqRequest"

  virtual_column   :type_display,                 :type => :string
  virtual_column   :template_valid,               :type => :boolean
  virtual_column   :template_valid_error_message, :type => :string
  virtual_column   :archived,                     :type => :boolean
  virtual_column   :active,                       :type => :boolean

  default_value_for :internal, false
  default_value_for :service_type, SERVICE_TYPE_ATOMIC
  default_value_for(:generic_subtype) { |st| 'custom' if st.prov_type == 'generic' }

  virtual_has_one :config_info, :class_name => "Hash"

  scope :with_service_template_catalog_id,          ->(cat_id) { where(:service_template_catalog_id => cat_id) }
  scope :without_service_template_catalog_id,       ->         { where(:service_template_catalog_id => nil) }
  scope :with_existent_service_template_catalog_id, ->         { where.not(:service_template_catalog_id => nil) }
  scope :displayed,                                 ->         { where(:display => true) }
  scope :public_service_templates,                  ->         { where(:display => true) }


  supports :order do
    unsupported_reason_add(:order, 'Service template does not belong to a service catalog') unless service_template_catalog
    unsupported_reason_add(:order, 'Service template is not configured to be displayed') unless display
  end
  alias orderable?     supports_order?
  alias validate_order supports_order?


  def self.with_tenant(tenant_id)
    tenant = Tenant.find(tenant_id)
    where(:tenant_id => tenant.ancestor_ids + [tenant_id])\
  end

Debug_inspector extension was not loaded rake aborted

I try install Canvas LMS on ubuntu 20.4 And I get the error when executing this command

commands:

sysadmin@appserver:var/canvas$ sudo yarn install sysadmin@appserver:/var/canvas$ sudo RAILS_ENV=production bundle exec rake canvas:compile_assets

Result:

debug_inspector extension was not loaded rake aborted! LoadError: incompatible library version - /var/canvas/vendor/bundle/ruby/2.7.0/gems/io-console-0.5.11/lib/io/console.so /var/canvas/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.4.7/lib/active_support/dependencies.rb:324:in require' /var/canvas/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.4.7/lib/active_support/dependencies.rb:324:in block in require' /var/canvas/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.4.7/lib/active_support/dependencies.rb:291:in load_dependency' /var/canvas/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.4.7/lib/active_support/dependencies.rb:324:in require' /var/canvas/vendor/bundle/ruby/2.7.0/gems/reline-0.3.1/lib/reline.rb:1:in <top (required)>' /var/canvas/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.4.7/lib/active_support/dependencies.rb:324:in require' /var/canvas/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.4.7/lib/active_support/dependencies.rb:324:in block in require' /var/canvas/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.4.7/lib/active_support/dependencies.rb:291:in load_dependency' /var/canvas/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.4.7/lib/active_support/dependencies.rb:324:in require' /var/canvas/vendor/bundle/ruby/2.7.0/gems/irb-1.4.1/lib/irb.rb:13:in <top (required)>' /var/lib/gems/2.7.0/gems/bundler-2.2.19/lib/bundler/runtime.rb:66:in require' /var/lib/gems/2.7.0/gems/bundler-2.2.19/lib/bundler/runtime.rb:66:in block (2 levels) in require' /var/lib/gems/2.7.0/gems/bundler-2.2.19/lib/bundler/runtime.rb:61:in each' /var/lib/gems/2.7.0/gems/bundler-2.2.19/lib/bundler/runtime.rb:61:in block in require' /var/lib/gems/2.7.0/gems/bundler-2.2.19/lib/bundler/runtime.rb:50:in each' /var/lib/gems/2.7.0/gems/bundler-2.2.19/lib/bundler/runtime.rb:50:in require' /var/lib/gems/2.7.0/gems/bundler-2.2.19/lib/bundler.rb:174:in require' /var/canvas/config/application.rb:28:in <top (required)>' /var/canvas/Rakefile:6:in require' /var/canvas/Rakefile:6:in <top (required)>' /var/canvas/vendor/bundle/ruby/2.7.0/gems/rake-13.0.3/exe/rake:27:in <top (required)>' /var/lib/gems/2.7.0/gems/bundler-2.2.19/lib/bundler/cli/exec.rb:63:in load' /var/lib/gems/2.7.0/gems/bundler-2.2.19/lib/bundler/cli/exec.rb:63:in kernel_load' /var/lib/gems/2.7.0/gems/bundler-2.2.19/lib/bundler/cli/exec.rb:28:in run' /var/lib/gems/2.7.0/gems/bundler-2.2.19/lib/bundler/cli.rb:474:in exec' /var/lib/gems/2.7.0/gems/bundler-2.2.19/lib/bundler/vendor/thor/lib/thor/command.rb:27:in run' /var/lib/gems/2.7.0/gems/bundler-2.2.19/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in invoke_command' /var/lib/gems/2.7.0/gems/bundler-2.2.19/lib/bundler/vendor/thor/lib/thor.rb:392:in dispatch' /var/lib/gems/2.7.0/gems/bundler-2.2.19/lib/bundler/cli.rb:30:in dispatch' /var/lib/gems/2.7.0/gems/bundler-2.2.19/lib/bundler/vendor/thor/lib/thor/base.rb:485:in start' /var/lib/gems/2.7.0/gems/bundler-2.2.19/lib/bundler/cli.rb:24:in start' /var/lib/gems/2.7.0/gems/bundler-2.2.19/exe/bundle:49:in block in <top (required)>' /var/lib/gems/2.7.0/gems/bundler-2.2.19/lib/bundler/friendly_errors.rb:130:in with_friendly_errors' /var/lib/gems/2.7.0/gems/bundler-2.2.19/exe/bundle:37:in <top (required)>' /usr/local/bin/bundle:23:in load' /usr/local/bin/bundle:23:in ' (See full trace by running task with --trace)

ruby version : 2.7.5 gem: 3.1.6

Could not find gem 'pg x64-mingw32' in locally installed gems

Please help me get rid of this error

I am running Ruby on Rails on Windows 10

Could not find gem 'pg x64-mingw32' in locally installed gems.enter image description here

Removing double quotes from the beging and end of an array in Ruby

I have an array stored in a file, when I read this file to variable in Ruby it returns double quotes at the beginning and end of the array. I use Ruby 2.7.0

irb
irb(main):065:0> ports = IO.read('/ports_values')
irb(main):066:0> ports
=> "['127.0.0.1:6601:6601', '127.0.0.1:8000:8000', '127.0.0.1:7200:7200', '127.0.0.1:9201:9201', '5606:5606', '6304:6504', '6305:6505']"

But what I need this:

irb(main):066:0> ports
    => ['127.0.0.1:6601:6601', '127.0.0.1:8000:8000', '127.0.0.1:7200:7200', '127.0.0.1:9201:9201', '5606:5606', '6304:6504', '6305:6505']

without double quotes and I tried this ,but it did not work

irb(main):067:0> ports.gsub /"/, ' '
=> "['127.0.0.1:6601:6601', '127.0.0.1:8000:8000', '127.0.0.1:7200:7200', '127.0.0.1:9201:9201', '5606:5606', '6304:6504', '6305:6505']"

jeudi 2 juin 2022

How can I import a file csv into the Rails database? [closed]

I want to be able to import a file csv to my database, could someone help me on how to do it? When importing the csv file I want it to be registered in my database. my db:

  class CreateData < ActiveRecord::Migration[5.2]
  def change
    create_table :data do |t|
      t.string :radio
      t.integer :net
      t.integer :mcc
      t.integer :area
      t.integer :cell
      t.float :latitude 
      t.float :length
      t.integer :range
      t.integer :samples
      t.integer :changeble
      t.timestamps
    end
  end
end

How do I separate the Mobile app user and web app user in REST API?

There are different user like Coach, Athlete, Meet Director and Admin. Coach & Athlete are mobile app users, Admin are Web app user, Meet Director are both mobile & web app user. Please suggest any gem or share your thoughts.