jeudi 27 décembre 2018

List all associated model records present in another model present in another namespace in rails

I have two models like:

class Superadmin::Company < ApplicationRecord
  belongs_to :user
  has_many :garments
end

2nd

class Garment < ApplicationRecord
   has_many :garments ,:class_name => "Superadmin::Company", dependent: :destroy
end

But when I search like

company = Superadmin::Company.find(9)
company.garments

Its give error: as

 Garment Load (1.3ms)  SELECT `garments`.* FROM `garments` WHERE `garments`.`company_id` = 9 ORDER BY created_at asc
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'garments.company_id' in 'where clause': SELECT `garments`.* FROM `garments` WHERE `garments`.`company_id` = 9 ORDER BY created_at asc
    from /home/tukatech/rails_projects/live_tukagarments/.bundle/gems/activerecord-5.0.7.1/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:218:in `query'

Table names in database is as:

1. garments
2. superadmin_companies

please provide if there is a correct way to search using rails foreign key associations relation.

Aucun commentaire:

Enregistrer un commentaire