奇怪的类型不匹配

时间:2019-02-09 00:03:03

标签: ruby-on-rails

FavoriteDealsPreferences之间的联接表,该关联在rails控制台中正常运行。

[6] pry(main)> p = Preference.first                                                                                           
  Preference Load (2.0ms)  SELECT  "preferences".* FROM "preferences" ORDER BY "preferences"."id" ASC LIMIT $1  [["LIMIT", 1]]
=> #<Preference:0x00007fb454b32f30
 id: 1,
 user_id: 2,
 created_at: Thu, 31 Jan 2019 20:21:51 UTC +00:00,
 updated_at: Thu, 31 Jan 2019 20:21:51 UTC +00:00>
[7] pry(main)> d = Deal.first                                                                                                 
  Deal Load (1.6ms)  SELECT  "deals".* FROM "deals" ORDER BY "deals"."id" ASC LIMIT $1  [["LIMIT", 1]]
=> #<Deal:0x00007fb453e05510
 id: 1,
 headline: "DCA to Des Moines, IA for $193 non-stop round trip - American Airlines",
 description:
  "This is a pretty great deal for a non-stop to DSM from DCA, but the departure dates are pretty limited from January 25 to February 25 (and not exactly daily but close). You can check out the calendar <a href='http://mail01.tinyletterapp.com/FaresYouCanUse/today-s-flights-iowa-new-hampshire-south-carolina-nevada/13473421-www.google.com/flights?c=cf040a38-8c96-4f9b-9338-8c223c053a53'>here</a>. After that, this route runs for more than $360 and sometimes more than $600! Though, prices are of course subject to change at any time, and I'm sure we'll see this fare fluctuate.",
 start_date: Fri, 25 Jan 2019,
 end_date: Mon, 25 Feb 2019,
 created_at: Fri, 25 Jan 2019 19:42:51 UTC +00:00,
 updated_at: Fri, 25 Jan 2019 20:28:12 UTC +00:00,
 instructions: nil,
 posted_date: Tue, 15 Jan 2019,
 blog_head:
  "I'm back on the grid - and bringing you a special 2020 Presidential Campaign Edition of Fares You Can Use.\n      <br><br>\n      I've been seeing some good deals for these particular locales for a while, and if you're planning some campaign travel (or to run for president), you may want to book these deals before the demand spike happens. \n      <br><br>\n      (I hope to provide an update on these for you every once in a while.)",
 blog_foot: "",
 region_id: 10,
 slug: "dca-to-des-moines-ia-for-193-non-stop-round-trip-american-airlines">
[8] pry(main)> f = Favorite.new deal:d, preference:p                                                                          
=> #<Favorite:0x00007fb45702fa18 id: nil, comment: nil, deal_id: 1, preference_id: 1, created_at: nil, updated_at: nil>

但是,在我的应用程序内部,当我尝试使用FavoriteDeal创建一个Preference时,出现了一个超现实的错误:

[3] pry(#<FavoritesController>)> f = Favorite.new(deal:d,preference:p)     
ActiveRecord::AssociationTypeMismatch: Preference(#70244671627000) expected, got #<Preference id: 1, user_id: 2, created_at: "2019-01-31 20:21:51", updated_at: "2019-01-31 20:21:51"> which is an instance of Preference(#70244669337180)

我正在使用的对象在下面,因此您可以“检查我的工作”。

请忽略PreferencePreferences之间的任何混淆。我已经控制住了。

[1] pry(#<FavoritesController>)> d = Deal.find_by(slug: params[:deal_slug] 
[1] pry(#<FavoritesController>)* )                                         
  Deal Load (0.8ms)  SELECT  "deals".* FROM "deals" WHERE "deals"."slug" = $1 LIMIT $2  [["slug", "iad-to-shanghai-for-403-round-trip-united-airlines"], ["LIMIT", 1]]
  ↳ (pry):8
=> #<Deal:0x00007fc63998eef8
 id: 5,
 headline: "IAD to Shanghai for $403 round trip - United Airlines",
 description:
  "This flight has one quick stop there and one quick stop on the way back, and is just about the best you can do for Shanghai. <i>And</i> all the flights in the route are operated by United, so you know exactly what you're getting! Lots of dates in February, March, and April. Find yours <a href='http://mail01.tinyletterapp.com/FaresYouCanUse/today-s-flights-shanghai-beijing-moscow-lisbon/13513653-www.google.com/flights?c=cf040a38-8c96-4f9b-9338-8c223c053a53'>here</a>. Make sure to check your flight times and who operates each flight before you book to confirm.",
 start_date: Fri, 01 Feb 2019,
 end_date: Tue, 30 Apr 2019,
 created_at: Fri, 25 Jan 2019 19:42:51 UTC +00:00,
 updated_at: Fri, 25 Jan 2019 20:28:12 UTC +00:00,
 instructions: nil,
 posted_date: Mon, 21 Jan 2019,
 blog_head: nil,
 blog_foot: nil,
 region_id: 3,
 slug: "iad-to-shanghai-for-403-round-trip-united-airlines">
[2] pry(#<FavoritesController>)> p = current_user.preferences              
  User Load (2.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 2], ["LIMIT", 1]]
  ↳ (pry):10
  Preference Load (0.8ms)  SELECT  "preferences".* FROM "preferences" WHERE "preferences"."user_id" = $1 LIMIT $2  [["user_id", 2], ["LIMIT", 1]]
  ↳ (pry):10
=> #<Preference:0x00007fc6373d5720
 id: 1,
 user_id: 2,
 created_at: Thu, 31 Jan 2019 20:21:51 UTC +00:00,
 updated_at: Thu, 31 Jan 2019 20:21:51 UTC +00:00>
[3] pry(#<FavoritesController>)> f = Favorite.new(deal:d,preference:p)     
ActiveRecord::AssociationTypeMismatch: Preference(#70244671627000) expected, got #<Preference id: 1, user_id: 2, created_at: "2019-01-31 20:21:51", updated_at: "2019-01-31 20:21:51"> which is an instance of Preference(#70244669337180)
from /Users/TuzsNewMacBook/.rvm/gems/ruby-2.3.7/gems/activerecord-5.2.2/lib/active_record/associations/association.rb:246:in `raise_on_type_mismatch!'

0 个答案:

没有答案