在Rails的导的表,我很困惑过为什么has_one
和has_many
表是相同的:混淆了对HAS_ONE和的has_many
customers(id,name)
orders(id,customer_id,order_date)
Example tables for has_one
: 这些在数据库层面,表还将允许供应商拥有多个帐户,但我们只需要每个供应商一个帐户
suppliers(id,name)
accounts(id,supplier_id,account_number) #Foreign Key to supplier here??
不应该为has_one
的表是这样的,而不是:
suppliers(id,name,account_id) #Foreign Key to account here
accounts(id,account_number)
现在因为account_id
是在供应商表,供应商不能有一个以上的账户。
Rails指南中的例子不正确吗?
或者,Rails是否使用has_many
类方法,但限制了many
部分的发生?
谢谢!这给了我一些思考的食物。所以,Rails基本上强制has_one中的“one”。我想知道,这是“安全”的。假定只有一个Rails应用程序将使用数据库,它应该没问题。 – Zabba 2010-10-17 06:44:55
Rails的关键指导原则之一是它专为应用程序数据库而非集成数据库而设计。这就是为什么在应用程序级别而不是数据库级别执行约束的原因。它驱动DBA坚果:) – 2010-10-17 17:28:41