我有模型:
class Topic < ActiveRecord::Base
attr_accessible :title, :description
has_many :comments, :dependent => :destroy
end
和:
class Comment < ActiveRecord::Base
attr_accessible :body, :topic_id
belongs_to :topic
end
在我的 topic_spec.rb
中:
it "should have the right associated comment" do
@topic.comments.should include(@comment)
end
it "should destroy associated comments" do
@topic.destroy
Comment.find_by_id(@comment.id).should be_nil
end
我收到以下错误:
1) Failure/Error: @topic.comments.should == @comment
NameError:
undefined method `inspect' for class `ActiveRecord::Associations::CollectionProxy'
2) Failure/Error: Comment.find_by_id(@comment.id).should be_nil
expected: nil
got: #<Comment id: 1, body: "first", created_at: "2011-08-18 09:55:06", updated_at: "2011-08-18 09:55:06">
我做错了什么?这个错误出现在我开始使用 sunspot 之后。
在我的 topic.rb 中:
searchable :auto_index => true, :auto_remove => true do
text :title, :boost => 5
text :description
text :comments do
comments.map(&:body)
end
end
如果我评论这行:
# text :comments do
# comments.map(&:body)
# end
所有测试成功通过!
最佳答案
问题是您正在使用 == 比较数组 (@topic.comments
) 和对象 (@comment
)。您应该检查该对象是否包含在数组中:
@topic.comments.should include(@comment)
关于ruby-on-rails - 太阳黑子的奇怪问题。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7105642/