Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
821 views
in Technique[技术] by (71.8m points)

activerecord - Rails error: object does not support #inspect

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...