所以这是我想要做的:
我想要一个基于文本的字段,“优惠券代码”
以嵌套的形式。
所以基本上f.text_field :coupon
但是该值应该是与coupons表中的项的关系,而不是通过id。
优惠券表格可能如下所示
|ID| CouponCode |
+--+------------+
|1 | a89sd9asda |
+--+------------+我的主表看起来像这样
|ID| OrderTitle | Coupon |
+--+------------+------------+
|1 | sdfsdfsdfd | a89sd9asda |
+--+------------+------------+在rails中有没有一种方法可以通过has_one连接这两个字段并使用非ID字段?
发布于 2012-04-26 06:12:03
回答你的问题:
class Order < ActiveRecord::Base
has_one :coupon_code, :foreign_key => 'CouponCode'
end
class CouponCode < ActiveRecord::Base
belongs_to :order, :foreign_key => 'CouponCode'
end 然而,我同意damien的观点:这看起来有点偏离了常见的db设计方法。
https://stackoverflow.com/questions/10324412
复制相似问题