我在MySQL数据库中有四个表。蛋白质,终止修饰,Nterms和Kws。 Kw_id是Terminusmodifications表中的外键。 terminusmodifications_id是Nterms表中的foreign_key。 protein_id是Nterms表中的外键,对应于Proteins表。
我有一个用户输入了kw_id。这应该能够给我一个Terminusmodifications_id,然后我可以在Nterms表上使用它。在Nterms表上使用它,我可以识别一个protein_id,我可以用它来查询蛋白质表。我想用这个查询来提取我已经得到的蛋白质表,看起来像这样:
@protein = Protein.joins(:searchnames).where("searchnames.name LIKE ?", "%#{params[:query]}%")
所以我在想:
@protein = @protein.joins(:nterms)....
这些方面的东西,但我不熟悉语法。我使用的是Rails 4.2和Ruby 2.0.0
以下是表格的样子:
Proteins:
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| ac | varchar(255) | YES | UNI | NULL | |
| name | varchar(255) | YES | UNI | NULL | |
| molecular_type | int(11) | YES | | NULL | |
| entry_type | varchar(255) | YES | | NULL | |
| dt_create | varchar(255) | YES | | NULL | |
| dt_sequence | varchar(255) | YES | | NULL | |
| dt_annotation | varchar(255) | YES | | NULL | |
| definition | varchar(255) | YES | | NULL | |
| sequence | text | YES | | NULL | |
| mw | int(11) | YES | | NULL | |
| crc64 | varchar(255) | YES | | NULL | |
| aalen | int(11) | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
| status | varchar(255) | YES | | unknown | |
| data_class | varchar(255) | YES | | NULL | |
| chromosome | varchar(255) | YES | MUL | NULL | |
| band | varchar(255) | YES | MUL | NULL | |
| species_id | int(11) | YES | MUL | NULL | |
| meropsfamily | varchar(255) | YES | MUL | NULL | |
| meropssubfamily | varchar(255) | YES | MUL | NULL | |
| meropscode | varchar(255) | YES | MUL | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
Nterms:
+-------------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| protein_id | int(11) | YES | MUL | NULL | |
| pos | int(11) | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
| import_id | int(11) | YES | MUL | NULL | |
| idstring | varchar(255) | YES | | NULL | |
| isoform_id | int(11) | YES | MUL | NULL | |
| terminusmodification_id | int(11) | YES | MUL | NULL | |
| seqexcerpt | varchar(255) | YES | MUL | NULL | |
+-------------------------+--------------+------+-----+---------+----------------+
Terminusmodifcations:
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | MUL | NULL | |
| description | text | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
| nterm | tinyint(1) | YES | | 0 | |
| cterm | tinyint(1) | YES | | 0 | |
| subcell | varchar(255) | YES | | NULL | |
| psimodid | varchar(255) | YES | | NULL | |
| display | tinyint(1) | YES | | 1 | |
| kw_id | int(11) | YES | MUL | NULL | |
| ac | varchar(255) | YES | MUL | NULL | |
+-------------+--------------+------+-----+---------+----------------+
Kws:
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | MUL | NULL | |
| ac | varchar(255) | YES | MUL | NULL | |
| description | text | YES | | NULL | |
| category | varchar(255) | YES | MUL | NULL | |
+-------------+--------------+------+-----+---------+----------------+
模型之间的关联:
蛋白质模型:
has_many :nterms, -> { uniq }
nterms模型:
belongs_to :protein
belongs_to :terminusmodification
终点修改模型:
has_many :nterms
belongs_to :kw
kw模特:
has_and_belongs_to_many :proteins
has_many :terminusmodifications
找到解决方案
@protein = @protein.joins(:nterms => {:terminusmodification => :kw}, :cterms => {:terminusmodification => :kw}).where("kws.name = ?", params[:modifications])