Ruby on rails 什么是合适的模型关联?
我有一个组织和行动代码模型。注册高级帐户时,组织可以输入折扣的操作代码。因此,一个组织没有使用任何或1个actioncode,而actioncode可以被许多组织使用 当前关联:Ruby on rails 什么是合适的模型关联?,ruby-on-rails,ruby,postgresql,ruby-on-rails-4,associations,Ruby On Rails,Ruby,Postgresql,Ruby On Rails 4,Associations,我有一个组织和行动代码模型。注册高级帐户时,组织可以输入折扣的操作代码。因此,一个组织没有使用任何或1个actioncode,而actioncode可以被许多组织使用 当前关联: Organizations migration file: t.references :actioncode, index: true, foreign_key: true Organization model: belongs_to :actioncode Actioncode mo
Organizations migration file: t.references :actioncode, index: true, foreign_key: true
Organization model: belongs_to :actioncode
Actioncode model: has_many :organizations
此设置的初始问题:actioncode_id不应是强制的在播种时,我用于获取错误代码ActiveRecord::AssociationTypeMismatch:actioncode(#23575660)预期,获取Fixnum(#5494220)
。原因似乎是actioncode_id已成为组织模型中的一个强制列。情况不应如此;actioncode也应该能够是nil
,因为组织在支付特优帐户时也可能不使用/拥有任何actioncode
当前情况:升级到PostgreSQL后:作为回应,我已将数据库升级到PostgreSQL。然而,现在在迁移时,我收到了错误消息(请参阅文章底部的完整消息):
PG::未定义:错误:关系“actioncodes”不存在
错误消息:知道是什么导致了此错误吗?删除“当前关联”下的三行,我在迁移和种子设定方面没有问题。事实上,保留所有三行,但从第一行中删除index:true、foreign\u key:true
,它也可以正确迁移。当我在迁移时保留index:true
时,它会给出错误:SyntaxError:xxx\u create\u organizations.rb:17:语法错误,意外的tSYMBEG,预期=>t.boolean:activated
。当我保留foreign\u key:true
时,它会生成原始错误消息PG::UndefinedTable
如果我将第一行更改为不正确的代码:t.references:actioncode\u id,index:true,foreign\u key:true
,它给出如下错误:
PG::UndefinedTable: ERROR: relation "actioncode_ids" does not exist
: ALTER TABLE "organizations" ADD CONSTRAINT "fk_rails_604f95d1a1"
FOREIGN KEY ("actioncode_id_id")
REFERENCES "actioncode_ids" ("id")
因此,在最后一行给出了_id之后,Rails似乎确实在表名方面存在问题。将self.table\u name='actioncodes'
添加到actioncode模型文件中没有任何区别。怎么办
替代协会: 我想知道我是否需要一个替代协会来满足我的需要。我已经调查了其他一些关联,但不确定该怎么办。最佳案例场景我只是在组织模型中有一列,其中包含使用的操作代码,如果没有使用操作代码,则为零。例如,我需要一个模型关联来打印所有使用特定操作代码的组织 备选关联1:此关联的问题是actioncode在组织模型中仍然是一个强制变量,因此不能为零
Organization migration: t.references :actioncode, index: true, foreign_key: true
Organization model: has_one :actioncode
Actioncode model: has_many :organizations
备选关联2:此关联将在actioncodes表中保存组织id(我猜是一个数组),而从我的角度来看,在organization表中包含actioncode id更有意义。此外,下面的代码仍然要求每个组织都有一个操作代码
Organization model: has_one :actioncode
Actioncodes migration file: t.belongs_to :organization, index: true
备选关联3:我也考虑过创建一个额外的关联模型,但这让我怀疑,当我遵循此策略时,我是否考虑过多,因为这将添加一个额外的表/模型。另外,我不确定下面的组织模式是否仍然需要折扣值,从而不能真正解决最初的问题。但如果这是一条路,我想:
Organization model: has_one :discount
has_one :actioncode, through: :discount
Discount model: belongs_to :organization
has_one :actioncode
Actioncode model: belongs_to :discount
Discount migration: t.belongs_to :organization, index: true
doesn;t need any other columns
Actioncode migration: t.belongs_to :discount, index: true
备选关联4:最后,has\u和\u属于\u-many关联对我来说没有意义,因为我对它的理解是针对多:多关系的,而organization:actioncode是多:1/0关系
我应该使用什么设置,并且没有将actioncode作为组织的强制变量?
其他信息:在
rake db:drop
和rake db:create
之后运行rake db:migrate
的SQL:
这里有一个逻辑/流程问题
组织
不应属于行动代码
,如果一个组织
可以拥有一个代码。或者不是。这就是为什么
组织机构
has_one :actioncode
行动码
belongs_to :organization
这样一来,Organization
将不会保存actioncode\u id
,但actioncode
将保存Organization\u id
更新:
按照我上面提到的操作,将Actioncode
posess设置为type
或value
字段,例如,该字段将包含'AC-08821'
字符串。这样,您可以保持这两个模型之间的预期关系,并且仍然能够搜索具有相同值的所有代码
或
创建一个关联表,例如使用has\u和\u-allown\u-to\u-many
更多信息请点击此处:
基本上,您可以创建另一个表来表示两个模型之间的连接。基于“…一个组织没有或只有一个actioncode,一个actioncode可以被许多组织使用”,您的建模是正确的,只是迁移创建的外键不允许actioncode_id列中出现null。您可以检查迁移SQL以查看使用了什么命令
也许您正在使用的数据库不允许零外键列,但大多数都允许。为了允许空值,您可能必须使用SQL而不是Rails来构造外键
从postgresql文档中:
现在不可能创建具有非空product\u的订单,因为products表中没有不显示的条目
请注意,“…对于非空产品\没有条目…”,空条目应该可以,并且您的建模是正确的。谢谢,这很有帮助。这对迁移文件意味着什么?这些应该如何使用c
has_one :actioncode
belongs_to :organization