我已經在我的DevicePurchase模型如下 -導軌和字符串協會造成AssociationTypeMismatch錯誤
has_one :coupon_code, primary_key: 'coupon_code', foreign_key: 'name'
的應用IM在聯營合作與優惠券的名稱(字符串,即「SummerPromo123」)一COUPONCODE一個DevicePurchase而不是這個想法。我在上面添加的關聯看起來是工作,但是當我試圖DevicePurchase.last.update(...)
我收到此 -
DevicePurchase.last.update(coupon_code: 'yLVDnw')
DevicePurchase Load (2.4ms) SELECT "device_purchases".* FROM "device_purchases" ORDER BY "device_purchases"."id" DESC LIMIT 1
(0.2ms) BEGIN
(0.3ms) ROLLBACK
ActiveRecord::AssociationTypeMismatch: CouponCode(#70224191762780) expected, got String(#70224129222500)
from /Users/ryangrush/.rvm/gems/[email protected]/gems/activerecord-4.0.12/lib/active_record/associations/association.rb:224:in `raise_on_type_mismatch!'
from /Users/ryangrush/.rvm/gems/[email protected]/gems/activerecord-4.0.12/lib/active_record/associations/has_one_association.rb:25:in `replace'
from /Users/ryangrush/.rvm/gems/[email protected]/gems/activerecord-4.0.12/lib/active_record/associations/singular_association.rb:17:in `writer'
from /Users/ryangrush/.rvm/gems/[email protected]/gems/activerecord-4.0.12/lib/active_record/associations/builder/association.rb:78:in `coupon_code='
from /Users/ryangrush/.rvm/gems/[email protected]/gems/activerecord-4.0.12/lib/active_record/attribute_assignment.rb:42:in `public_send'
from /Users/ryangrush/.rvm/gems/[email protected]/gems/activerecord-4.0.12/lib/active_record/attribute_assignment.rb:42:in `_assign_attribute'
from /Users/ryangrush/.rvm/gems/[email protected]/gems/activerecord-4.0.12/lib/active_record/attribute_assignment.rb:29:in `block in assign_attributes'
from /Users/ryangrush/.rvm/gems/[email protected]/gems/activerecord-4.0.12/lib/active_record/attribute_assignment.rb:23:in `each'
from /Users/ryangrush/.rvm/gems/[email protected]/gems/activerecord-4.0.12/lib/active_record/attribute_assignment.rb:23:in `assign_attributes'
from /Users/ryangrush/.rvm/gems/[email protected]/gems/activerecord-4.0.12/lib/active_record/persistence.rb:230:in `block in update'
from /Users/ryangrush/.rvm/gems/[email protected]/gems/activerecord-4.0.12/lib/active_record/transactions.rb:330:in `block in with_transaction_returning_status'
from /Users/ryangrush/.rvm/gems/[email protected]/gems/activerecord-4.0.12/lib/active_record/connection_adapters/abstract/database_statements.rb:203:in `block in transaction'
from /Users/ryangrush/.rvm/gems/[email protected]/gems/activerecord-4.0.12/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `within_new_transaction'
from /Users/ryangrush/.rvm/gems/[email protected]/gems/activerecord-4.0.12/lib/active_record/connection_adapters/abstract/database_statements.rb:203:in `transaction'
from /Users/ryangrush/.rvm/gems/[email protected]/gems/activerecord-4.0.12/lib/active_record/transactions.rb:209:in `transaction'
from /Users/ryangrush/.rvm/gems/[email protected]/gems/activerecord-4.0.12/lib/active_record/transactions.rb:327:in `with_transaction_returning_status'
from /Users/ryangrush/.rvm/gems/[email protected]/gems/activerecord-4.0.12/lib/active_record/persistence.rb:229:in `update'
from (irb):55
from /Users/ryangrush/.rvm/gems/[email protected]/gems/railties-4.0.12/lib/rails/commands/console.rb:90:in `start'
from /Users/ryangrush/.rvm/gems/[email protected]/gems/railties-4.0.12/lib/rails/commands/console.rb:9:in `start'
from /Users/ryangrush/.rvm/gems/[email protected]/gems/railties-4.0.12/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'2.2.4 :056 >
我發現這個錯誤幾個StackOverflow的答案,但我認爲這個問題是與我聯繫。
**更新**
如果我跑DevicePurchase.last.update(coupon_code: CouponCode.last)
我碰到下面的錯誤 -
SELECT 1 AS one FROM "coupon_codes" WHERE ("coupon_codes"."name" = 1188 AND "coupon_codes"."id" != 1188) LIMIT 1
PG::UndefinedFunction: ERROR: operator does not exist: text = integer
LINE 1: ...FROM "coupon_codes" WHERE ("coupon_codes"."name" = 1188 AND...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT 1 AS one FROM "coupon_codes" WHERE ("coupon_codes"."name" = 1188 AND "coupon_codes"."id" != 1188) LIMIT 1
(0.2ms) ROLLBACK
ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR: operator does not exist: text = integer
LINE 1: ...FROM "coupon_codes" WHERE ("coupon_codes"."name" = 1188 AND...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
顯示引發錯誤的完整代碼。該代碼將幫助我們幫助你。 :D –
確定它現在更新。 –
嘗試'DevicePurchase.last.update(coupon_code:Coupon.find_by_name('yLVDnw'))'..我的意思是給它的實際'Coupon'對象..告訴我 –