我有两个模型,例如:
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
它给出错误: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'
数据库中的表名称为:
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)
答案 0 :(得分:2)
根据帖子中提到的描述和答案之一中的注释,似乎模型中定义的关系无法与列名相关。
要使其正常运行,请更改为以下之一:
class Superadmin::Company < ApplicationRecord
belongs_to :user
has_many :garments, class_name: "Garment", foreign_key: "superadmin_company_id"
end
现在它将开始使用关系中指定的foreign_key映射。
答案 1 :(得分:1)
更新关联如下:
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
答案 2 :(得分:0)
delete
表中没有company_id
列。您必须通过迁移添加它。试试:
garments