2016-08-04 48 views
0

Ich habe folgende in meinem DevicePurchase Modell -Rails Assoziationen mit String verursacht AssociationTypeMismatch Fehler

has_one :coupon_code, primary_key: 'coupon_code', foreign_key: 'name' 

Die App Im mit einem Coupon durch den Coupon Namen auf Mitarbeiter ein DevicePurchase arbeiten (eine Zeichenfolge, dh "SummerPromo123") und nicht die Idee. Der Verein ich oben hinzugefügt sah jedoch zu arbeiten, als ich versuchte, DevicePurchase.last.update(...) erhielt ich diese -

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 > 

fand ich ein paar Antwort Stackoverflow für diesen Fehler, aber ich denke, das Problem mit meinem Verein ist.

** Update **

Wenn ich laufe DevicePurchase.last.update(coupon_code: CouponCode.last) bekomme ich folgende Fehler -

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. 
+0

Zeigen Sie den vollständigen Code, der Fehler auslöst. Dieser Code hilft uns, Ihnen zu helfen. : D –

+0

ok es ist jetzt aktualisiert. –

+0

versuchen 'DevicePurchase.last.update (coupon_code: Coupon.find_by_name ('yLVDnw'))' .. Ich meine, geben Sie es die eigentliche 'Coupon'-Objekt .. sagen Sie mir –

Antwort

0

Ok ist die Frage, Sie haben den Vereinsnamen und Spaltennamen identisch. Also, wenn Sie versuchen, DevicePurchase.last.update(coupon_code: 'yLVDnw'), Rails denkt, Sie werden es ein CouponCode Objekt übergeben, nicht die "Zeichenfolge" Wert, wie Sie jetzt haben. Deshalb hast du den Fehler bekommen. Aber wenn Sie die Zuordnung wie folgt umbenennen, sollten Sie in Ordnung sein:

has_one :coupon, 
    primary_key: 'coupon_code', 
    foreign_key: 'name', 
    class_name: 'CouponCode' 
+1

das funktioniert, danke! –