0

So heres what I am trying to do:

I want a field that is text based, "a coupon code"

in a nested form.

So basically f.text_field :coupon

But that value should be a relationship to an item in a coupons table, but not by id.

Coupon table could look like

|ID| CouponCode |
+--+------------+
|1 | a89sd9asda |
+--+------------+

and my main table would look like

|ID| OrderTitle |   Coupon   |
+--+------------+------------+
|1 | sdfsdfsdfd | a89sd9asda |
+--+------------+------------+

Is there a way in rails to link these two via a has_one and use a non ID field?

4

1 回答 1

1

To answer your question:

class Order < ActiveRecord::Base
   has_one :coupon_code, :foreign_key => 'CouponCode'
end

class CouponCode < ActiveRecord::Base
   belongs_to :order, :foreign_key => 'CouponCode'
end    

However, I agree with damien: this looks a bit off of the common db design approaches.

于 2012-04-25T22:12:02.847 回答