j_mcnally
j_mcnally

Reputation: 6958

Custom lookup field on rails 3 form

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?

Upvotes: 0

Views: 308

Answers (1)

forker
forker

Reputation: 2679

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.

Upvotes: 1

Related Questions