列出 rails 中另一个名称空间中存在的另一个模型中存在的所有关联模型记录
List all associated model records present in another model present in another namespace in rails
我有两个模型:
class Superadmin::Company < ApplicationRecord
belongs_to :user
has_many :garments
end
第二
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'
Table 数据库中的名称为:
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)
garments
table 中没有 company_id
列。您必须通过迁移添加它。尝试:
rails generate migration AddCompanyToGarment company:references
根据 post 中提到的描述和其中一个答案中的评论,模型中定义的关系似乎无法与列名关联。
要使其正常工作,请更改为以下:
class Superadmin::Company < ApplicationRecord
belongs_to :user
has_many :garments, class_name: "Garment", foreign_key: "superadmin_company_id"
end
现在它将开始映射关系中指定的 foreign_key。
更新关联如下:
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
我有两个模型:
class Superadmin::Company < ApplicationRecord
belongs_to :user
has_many :garments
end
第二
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'
Table 数据库中的名称为:
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)
garments
table 中没有 company_id
列。您必须通过迁移添加它。尝试:
rails generate migration AddCompanyToGarment company:references
根据 post 中提到的描述和其中一个答案中的评论,模型中定义的关系似乎无法与列名关联。
要使其正常工作,请更改为以下:
class Superadmin::Company < ApplicationRecord
belongs_to :user
has_many :garments, class_name: "Garment", foreign_key: "superadmin_company_id"
end
现在它将开始映射关系中指定的 foreign_key。
更新关联如下:
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