English 中文(简体)
使用 NULLL id 的 sql 语句
原标题:has_many :through - sql statement using NULL id

我们最近升级了我们的铁路应用程序,从3.0.3版本升级到3.1.0版本。 应用程序与以前一样, 大部分运行成功, 但有一个重大例外。 我们在两个模型( SurveyDatum 和 子组)之间存在许多对许多的关系, 通过一个名为子组SUveiveDatum 的模型加入。 以下是每个模型的代码 :

class SurveyDatum < ActiveRecord::Base
  has_many :sub_group_survey_data
  has_many :sub_groups, :through => :sub_group_survey_data
end


class SubGroup < ActiveRecord::Base
  has_many :sub_group_survey_data
  has_many :survey_data, :through => :sub_group_survey_data
end

正如你所期待的:

class SubGroupSurveyDatum < ActiveRecord::Base
  belongs_to :survey_datum
  belongs_to :sub_group
end

如果有我先前从数据库中检索到的 SurveyDatum 对象( 叫做 sd ), 并且我使用子组方法( sd. sub_ groups), 这是通过主动记录生成的 Sql 查询结果 :

SELECT `sub_groups`.* FROM `sub_groups` INNER JOIN `sub_group_survey_data` ON `sub_groups`.`id` = `sub_group_survey_data`.`sub_group_id` WHERE `sub_group_survey_data`.`survey_datum_id` IS NULL

“ IS NULL” 部分显然是我的勘测数据对象的代号应该去的地方, 不管主动记录没有使用它。 该对象确实有一个代号, 因为如前所述, 它一直存在, 并从数据库中检索。 这个问题只是在我们搬到铁路3.1 之后才出现, 所以我假设有些事情我没有按照新版本做正确, 但我不知道。 任何想法吗? 感谢您的帮助!

最佳回答

我们发现了这个问题,我忘记了调查数据表有一个综合主钥匙。当我们更新到3.2.3版本并在调查达顿模型中添加以下内容时:

set_primary_key :id

查询最终正确构建和执行 。

问题回答

Hmm 我用了3.1.0的轨迹,试图复制,但一切都很好。唯一的例子是,我手动在从Db上检索的记录上设置了id = 零。 然后我得到了:

SELECT "authors".* FROM "authors" INNER JOIN "relations" ON "authors"."id" = "relations"."author_id" WHERE "relations"."post_id" IS NULL

我用Sqlite3尝试过这个 还要注意某些宝石 尤其是那些在积极记录公司工作的宝石





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签