0

我有两个简单的模型,ProjectSetting 和 ProjectSettingQuestions。ProjectSettingQuestions 属于 ProjectSetting。我想在查询 ProjectSetting 时加载 ProjectSettingQuestions 数据。似乎不可能用这个查询做到这一点:

 ProjectSetting.includes(:project_setting_questions).where(:project_id=>params[:project_id])

参数不是问题。此行获取 ProjectSetting 数据,但不获取问题。谢谢!

日志显示以下内容:

ProjectSetting Load (0.3ms)  SELECT "project_settings".* FROM "project_settings" WHERE    "project_settings"."project_id" = 31 LIMIT 1
ProjectSettingQuestion Load (0.4ms)  SELECT "project_setting_questions".* FROM "project_setting_questions" WHERE "project_setting_questions"."project_setting_id" IN (2)
4

1 回答 1

0

假设你有has_manybelongs_to关系,你可以这样做:

project_setting = ProjectSetting.first
project_setting.project_setting_questions

或者你也可以这样做,因为ProjectSettingQuestionsbelongs_to ProjectSetting,它包含foreign_keyofProjectSetting

project_setting = ProjectSetting.first
ProjectSettingQuestion.where(:project_setting_id => project_setting.id)

如果您还没有看过本指南,您应该看看:http: //guides.rubyonrails.org/association_basics.html

希望能帮助到你。

于 2013-04-01T03:57:37.317 回答