列出出现在另一本模型中的所有关联的模型记录在轨另一个命名空间(List all associated

2019-10-29 11:25发布

我有两个模型,如:

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

第2

class Garment < ApplicationRecord
   belongs_to :company ,:class_name => "Superadmin::Company"
end

但是,当我像搜索

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

它给予错误:如

 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'

在数据库表名是:

1. garments
2. superadmin_companies

请提供是否有使用Rails外键关联关系搜索的正确道路。

数据的基础上是为:

mysql> desc superadmin_companies;
+-------------------+--------------+------+-----+---------+----------------+
| Field             | Type         | Null | Key | Default | Extra          |
+-------------------+--------------+------+-----+---------+----------------+
| id                | int(11)      | NO   | PRI | NULL    | auto_increment |
| name              | varchar(255) | YES  |     | NULL    |                |
| address           | varchar(255) | YES  |     | NULL    |                |
| phone             | varchar(255) | YES  |     | NULL    |                |
| user_id           | int(11)      | YES  | MUL | NULL    |                |
| created_at        | datetime     | NO   |     | NULL    |                |
| updated_at        | datetime     | NO   |     | NULL    |                |
| logo_file_name    | varchar(255) | YES  |     | NULL    |                |
| logo_content_type | varchar(255) | YES  |     | NULL    |                |
| logo_file_size    | int(11)      | YES  |     | NULL    |                |
| logo_updated_at   | datetime     | YES  |     | NULL    |                |
+-------------------+--------------+------+-----+---------+----------------+
11 rows in set (0.00 sec)

mysql> desc garments;
+--------------------------+--------------+------+-----+---------+----------------+
| Field                    | Type         | Null | Key | Default | Extra          |
+--------------------------+--------------+------+-----+---------+----------------+
| id                       | int(11)      | NO   | PRI | NULL    | auto_increment |
| xhtml_file_file_name     | varchar(255) | YES  |     | NULL    |                |
| xhtml_file_content_type  | varchar(255) | YES  |     | NULL    |                |
| xhtml_file_file_size     | int(11)      | YES  |     | NULL    |                |
| xhtml_file_updated_at    | datetime     | YES  |     | NULL    |                |
| xhtml_thumb_file_name    | varchar(255) | YES  |     | NULL    |                |
| xhtml_thumb_content_type | varchar(255) | YES  |     | NULL    |                |
| xhtml_thumb_file_size    | int(11)      | YES  |     | NULL    |                |
| xhtml_thumb_updated_at   | datetime     | YES  |     | NULL    |                |
| created_at               | datetime     | NO   |     | NULL    |                |
| updated_at               | datetime     | NO   |     | NULL    |                |
| category                 | varchar(255) | YES  |     | NULL    |                |
| garment_type             | varchar(255) | YES  |     | NULL    |                |
| user_id                  | int(11)      | YES  |     | NULL    |                |
| superadmin_company_id    | int(11)      | YES  | MUL | NULL    |                |
+--------------------------+--------------+------+-----+---------+----------------+
15 rows in set (0.00 sec)

Answer 1:

按照帖子中提到的描述和它看起来像在模型中定义的关系是无法与列名相关答案之一的意见。

对于它的工作,请更改为以下之一:

class Superadmin::Company < ApplicationRecord
  belongs_to :user
  has_many :garments, class_name: "Garment", foreign_key: "superadmin_company_id"
end

现在,它将开始在关系中指定的foreign_key映射。



Answer 2:

更新关联如下:

class Superadmin::Company < ApplicationRecord
  has_many :garments, foreign_key: 'superadmin_company_id'
end

class Garment < ApplicationRecord
  belongs_to :company, class_name: 'Superadmin::Company', foreign_key: 'superadmin_company_id'
end


Answer 3:

没有company_idgarments表。 您可以通过迁移来添加它。 尝试:

rails generate migration AddCompanyToGarment company:references


文章来源: List all associated model records present in another model present in another namespace in rails