Rails error: object does not support #inspect

心已入冬 提交于 2019-12-11 16:44:56

问题


Scratching my head here... If I try the following with rails:

User.limit(25).includes([:addresses])

I get:

 User Load (109.5ms)  EXEC sp_executesql N'SELECT TOP (25) [User].* FROM [User]'
 Address Load (112.3ms)  EXEC sp_executesql N'SELECT [Address].* FROM [Address] WHERE [Address].[UserId] IN (N''1'', N''2'', N''3'', N''6'', N''7'', N''8'', N''9'', N''11'', N''12'', N''16'', N''17'', N''18'', N''19'', N''20'', N''21'', N''22'', N''24'', N''25'', N''26'', N''27'', N''28'', N''29'', N''30'', N''31'', N''34'')'
(Object doesn't support #inspect)
 =>  

If I instead do

@users = User.limit(25).joins([:addresses])

this works fine, however an inner join is not what I want not to mention I'm actually trying to do this through sunspot_rails which only supports includes.

I'm not entirely sure what is up with this error. I suspect it might be due to the fact that this is a legacy MSSQL database so the naming conventions are nowhere near what rails likes. However I'm at a loss as to what the specific issue is.

The models are as follows, stripped down to the minimum which is verified to reproduce the problem:

class User < ActiveRecord::Base
  set_table_name "User"
  set_primary_key "ID"

  has_many :addresses, :foreign_key => 'UserId'
end

class Address < ActiveRecord::Base
  set_table_name "Address"
  set_primary_key "ID"
  belongs_to :user
end

That is it. There is other code on the user model for searching and authentication, but I've commented that all out and verified it has no impact on this problem. I'm stuck on this, any help is appreciated. The app is using rails 3.1, with activerecord-sqlserver-adapter and tiny_tds.


回答1:


Solved...

:foreign_key => "UserID", not :foreign_key => "UserId"

For some reason, the case difference does not seem to bother rails when doing non eager loading, or with .joins, but does with .includes. Doh.



来源:https://stackoverflow.com/questions/7898735/rails-error-object-does-not-support-inspect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!