我很茫然。我们有两个类,Sample和Workflow,它们具有多对多的关系,使用has_many,:through。
在Rails控制台,我得到以下内容:
[3] pry(main)> s = Sample.find(1)
Sample Load (0.2ms) SELECT `samples`.* FROM `samples` WHERE `samples`.`id` = 1 LIMIT 1
=> #<Sample id: 1, name: "Some sample", experiment_type: "Some type", category_id: nil,
created_at: "2014-01-14 21:24:14", updated_at: "2014-01-14 21:24:14">
[4] pry(main)> wf = Workflow.new
=> #<Workflow id: nil, name: nil, description: nil>
[5] pry(main)> wf.samples.include?(s)
Sample Exists (0.2ms) SELECT 1 AS one FROM `samples` INNER JOIN `sample_workflows` ON
`samples`.`id` = `sample_workflows`.`sample_id` WHERE `sample_workflows`.`workflow_id` IS NULL
AND `samples`.`id` = 1 LIMIT 1
=> true
[6] pry(main)> wf.samples
=> []
[7] pry(main)> wf.samples.include?(s)
=> false
wf是一个全新的工作流程,不应附加任何样本。 wf.samples.include?表示样本1已链接到工作流程。但是,如果我试着查看样本,那么阵列会变回空白,还包括?查询返回false(因为它们应该)。对于样本2也会发生同样的事情,但对于我尝试过的其他任何事情都没有。有没有什么方法我可以默认错误地将工作流链接到两个样本?