Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/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
Activerecord 有很多或加入-什么';这是';轨道';要用我的桌子吗?_Activerecord_Ruby On Rails 4 - Fatal编程技术网

Activerecord 有很多或加入-什么';这是';轨道';要用我的桌子吗?

Activerecord 有很多或加入-什么';这是';轨道';要用我的桌子吗?,activerecord,ruby-on-rails-4,Activerecord,Ruby On Rails 4,我有一个记录事故的数据库。每次事故都可能有多种原因。每个原因在第三个表中都有一个友好的名称。创建此关联的“rails方式”是什么 以下是我所拥有的: create_table "accidents", force: true do |t| t.text "notes" t.datetime "created_at" t.datetime "updated_at" end create_table "causes", force: true do |t| t.intege

我有一个记录事故的数据库。每次事故都可能有多种原因。每个原因在第三个表中都有一个友好的名称。创建此关联的“rails方式”是什么

以下是我所拥有的:

create_table "accidents", force: true do |t|
  t.text     "notes"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "causes", force: true do |t|
  t.integer  "accident_id"
  t.integer  "cause_name_id"
  t.datetime "created_at"
  t.datetime "updated_at"
end

create_table "cause_names", force: true do |t|
  t.string   "name"
  t.datetime "created_at"
  t.datetime "updated_at"
end
CauseName.create :name => "DUI"
CauseName.create :name => "Speeding"
事故:

class Accident ActiveRecord::Base
  has_many :causes
end
原因:

class Cause < ActiveRecord::Base
  belongs_to :accidents
  has_one :cause_name
end
我一直在尝试各种各样的事情,但我似乎无法让我的联想按我预期的方式运作。我知道我用得不对

我对rails和activerecord非常陌生,对数据库也很糟糕。。。我正在使用的模式是由我们的dba设计的,他将在表上做报告,但对Ruby或ActiveRecord一无所知


在我的情况下,最好的方法是什么?我用对了吗?

在你的情况下,我建议不要将原因分成两个表。只需将
name
塞进原因表,就可以结束了


然后你可以很容易地查询像
@incident.causes.first.name

我想你的
属于
并且
有一个
方法错误地放置在你的
Cause-CauseName
关联中

引述:

如果要在两个模型之间建立一对一的关系, 您需要将“属于”添加到一个,并将“属于”添加到另一个。怎么办 你知道哪个是哪个吗

区别在于外键的放置位置(它位于 声明所属关联的类的表),但是 也应该考虑数据的实际意义。 has_one的关系说某样东西中有一个是你的——那 就是,有东西指向你

所以,在你的情况下,应该是这样的:

Accident.causes.first.cause_name.name #speeding
a = CauseName.find(1)
Accident.causes.first.cause_name = a #set and saved successfully
class CauseName < ActiveRecord::Base
  has_one :cause # Note that I have changed :causes to singular too
end

class Cause < ActiveRecord::Base
  belongs_to :accident # <- Singularized too
  belongs_to :cause_name
end
类CauseName属于:事故——我们首先爆发事故的部分原因是为了保持随着时间的推移稍微调整原因名称的能力。例如,我们可以从“超速”改为“超速摩托车”,并添加“超速汽车”。显然,在一个真实的例子中,我们会完全为automobile_类型添加另一个列,但是您得到了一般的想法。仅仅在表中保存简单的语言字符串“speeding”/“DUI”是否设计糟糕?似乎它不具有可伸缩性?你的意见是什么?你是对的。若你们想管理原因的名称,你们所做的并没有错。别忘了给外键和
name
添加索引。这正是我出错的地方。另外,当使用has_one和belish_to时,记住使用单数引用也是很重要的,这是我弄错的。我很惊讶,当我犯这个错误时,ActiveRecord没有对我吼叫。我不确定是因为它为我修复了它,还是因为它默默地失败了。古怪的
class CauseName < ActiveRecord::Base
  has_one :cause # Note that I have changed :causes to singular too
end

class Cause < ActiveRecord::Base
  belongs_to :accident # <- Singularized too
  belongs_to :cause_name
end