Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在rails活动记录中捕获创建的ID并比较它们的属性_Ruby On Rails - Fatal编程技术网

Ruby on rails 如何在rails活动记录中捕获创建的ID并比较它们的属性

Ruby on rails 如何在rails活动记录中捕获创建的ID并比较它们的属性,ruby-on-rails,Ruby On Rails,我有一个问题模型和一个可能的回答模型。表格如下 可能的答案 create_table "possible_answers", force: :cascade do |t| t.integer "question_id" t.string "title" end create_table "questions", force: :cascade do |t| t.string "title" t.string "right_answer" end

我有一个问题模型和一个可能的回答模型。表格如下

可能的答案

create_table "possible_answers", force: :cascade do |t|
    t.integer  "question_id"
    t.string   "title"
end
create_table "questions", force: :cascade do |t|
    t.string   "title"
    t.string   "right_answer"
end
问题

create_table "possible_answers", force: :cascade do |t|
    t.integer  "question_id"
    t.string   "title"
end
create_table "questions", force: :cascade do |t|
    t.string   "title"
    t.string   "right_answer"
end
关联是(你可能已经猜到了,但无论如何)

问题.rb

class Question < ActiveRecord::Base
  has_many :possible_answers
end 
这里的
可能的答案\u id
是用户选择的选项,可能的答案属于
问题\u id

我的问题是,如何将这个可能的答案id的
标题
与问题id的
正确答案
进行比较

我尝试使用find params并传递id,但没有成功。
谁能帮我一下吗。我在构建这个应用程序方面走了很长一段路,但现在仍停留在这里。

您是否考虑过在您的
可能答案表中添加
:correct、:boolean、default::false
。这样,当用户选择答案时,您就可以找到它,并查看它是否正确

问题
可能的答案
中同时包含答案字符串并不是最有效的解决方案

或者(如果您想将答案保留在问题表中),您可以将
right\u-answer
更改为
appability\u-answer\u-id
,并将
has\u-one:appability\u-answer更改为::right\u-answer
。因此,您可以:

question.possible_answers   # returns list
question.right_answer       # returns just the single record of the correct answer

为什么问题中的
答案不正确\u id
?这样你就可以直接比较身份证了。而且,“没用”也没告诉我们什么。什么不起作用(确切代码)?怎么没用?请看一看页面和这一页:。我宁愿比较值而不是ID。我将更新我尝试过的内容。@user3576036如果要比较值,这意味着您必须使用相同的比较逻辑确保
正确答案始终等于
可能答案的标题(是否区分大小写、是否分隔空格等)。与简单的id匹配测试相比,这似乎相当痛苦。
right\u-answer
问题
表下的一列。是的,我建议删除此项。在我的解决方案中,right\u-answer将从
可能的答案
表返回正确的记录您希望我从
中删除
right\u-answer
列吗>问题
表格,而不是
问题
可能答案
?@user3576036这个结构允许一个
问题
有几个
正确答案
s(获得了灵活性)。此外,您可以简单地在问题
中定义有多个:正确答案,{where(correct:true)}
并将给定的
可能答案
'id与该问题的
正确答案
'id轻松进行比较
question.possible_answers   # returns list
question.right_answer       # returns just the single record of the correct answer