sunspot_solr - NoMethodError (undefined method `result=' for nil:NilClass) - ruby-on-rails-3

I have a problem related to the use of sunspot_solr. I have an application that performs some searches using this gem. There are 5 models that has this feature, only 1 of them is showing an error in
#search.results
NoMethodError (undefined method `result=' for nil:NilClass):
app/controllers/financial_dashboards_controller.rb:27:in `index'
Here is my code:
Controller
#search = Sunspot.search(Purchase) do
fulltext params[:search]
with(:product_owner).equal_to(current_user.id)
facet(:status)
if params[:status].present?
with(:status).equal_to(params[:status])
end
facet(:sell_date)
if params[:sell_date].present?
with(:sell_date).equal_to(params[:sell_date])
end
order_by(:sell_date, :desc)
end
#line 27
#sales = #search.results.paginate(:page => params[:page], :per_page => 5)
Model (Purchase):
searchable do
text :product_name
text :product_short_description
integer :product_owner
string :status
string :sell_date
end
def product_name
product.name
end
def product_short_description
product.short_description
end
def product_owner
product.user.id
end
def sell_date
date.to_s(:year_month)
end
#indicates status of the payment and delivery
def status()
if !self.closed.nil?
I18n.t('purchases.status.finished')
elsif !self.measured.nil?
I18n.t('purchases.status.measured')
elsif !self.accomplished.nil?
I18n.t('purchases.status.delivered')
elsif !self.paid.nil?
I18n.t('purchases.status.paid')
elsif !self.canceled.nil?
I18n.t('purchases.status.canceled')
elsif !self.date.nil?
I18n.t('purchases.status.waiting_payment')
end
end
Another strange thing is that on my development machine, this code works perfectly. On production machine that uses nginx, the code displays this error.
I checked the version of gems and they match. I tried
rake sunspot:solr:reindex RAILS_ENV=production
to reindex. I tried
rake sunspot:solr:stop RAILS_ENV=production
rake sunspot:solr:start RAILS_ENV=production
to restart the search server. Even tried to remove solr/ folder and let the start script copy it again.
And why the other models work perfectly? Any ideas how to solve this?
Thanks

In my case, it was a situation when the key field (id) was not unique.
It happened because I have designed a mysql view with non distinct id field.
And that is why sunspot always "dropped" first hit to nil during next non unique row indexation.
So
hit.result = result
raised an error somewhere in sunspot gem code
When i figured it out (i've spent few hours on it), I just have made my id field unique, reindexed my models and problem have gone.

Related

Solr reindex with associations

I have the following code in my Model:
has_many :links
searchable do
text :name, :as => :name_textp
integer :membership_ordering
text :business_description, :stored => true
text :tags do
tags.map(&:name)
end
text :links_name do
company_links.map(&:name)
end
text :links_description do
company_links.map(&:description)
end
text :links_tags do
links.map(&:tags_text)
end
end
When for some reason I update the model, Solr automatically add the change to the index. But when I update links , it seems it doesn't until I manually call the sunspot:solr:reindex. I also added the following code to the association:
searchable do
text :name
text :description
text :tags_text
end
But I can't get it work. What else do I need to do?
have you been installing solr from scratch or with bundle install command
Please have a look to this procedure, I think that will make works your ruby / solr configuration.
Prefere to launch solr with :
bundle exec rake sunspot:solr:start
bundle exec rake sunspot:reindex
Enjoy :)
UPDATE 6/06 1H49 BRT
Do you have set autocommit in your solrconfig.xml:
<autoCommit>
<maxDocs>10000</maxDocs>
<maxTime>20000</maxTime>
</autoCommit>
UPDATE 6/06 9H41 BRT
Please take a look a this post: sunset not reindexing it seems to be a common issue.
Can be overpassed by performing a commit.
As dzone says :
you will need regular reindexing with sunspot.

Rails - find_by_* route

In products_controller.rb
# GET /search/'brand'
def brand
#product = Product.find_all_by_brand(params[:brand])
respond_to do |format|
format.html # brand.html.erb
end
end
In routes.rb
match '/search/:brand' => 'products#brand'
If I try accessing localhost:3000/search/Apple I get the following error Couldn't find Product with id=Apple
Is there anything that I'm missing? Are there any other files that I should handle?
Update
Now I'm getting undefined method 'size' for nil:NilClass and I'm not even sure what I changed.
The query executed by rails is select "products".* FROM "products" WHERE "products"."brand" = 'Apple' ORDER BY last_seen DESC and they seem to return the correct products.
Application Trace
app/views/products/_product.html.erb:1:in
_app_views_products__product_html_erb___2255278_29707176'
app/views/products/brand.html.erb:1:in
_app_views_products_brand_html_erb___464952485_38589588'
app/controllers/products_controller.rb:52:in `brand'
The comments in your controller indicate that you might want to try /search/brand/Apple.
Found the error, the erb page was receiving product instead of products. Typing error.

My delete buttons broke - Error with ActiveRecord

This was working perfectly and now I'm not sure what happened.
Here are the errors:
ActiveRecord::RecordNotFound at /status_updates/92
Couldn't find User with id=92
and
ActiveRecord::RecordNotFound at /status_updates/delete_all
Couldn't find User with id=delete_all
What happens is I have users that have many status updates. 92 here should refer to status update 92 that belongs to this user.
I haven't changed any of my code directly relating to this so I suspect it has something to do with a change I tried to make with my coffee-script. (I removed it from my gem file, ran bundle install, and then decided to try to live with out removing it, so I put it back in the gem file, ran bundle install again... and that's it.)
Anyway, the error occurs when I'm in show.html and I'm clicking either of my delete buttons. Either the delete_all or delete for a singular status_update.
Here's the controller methods for all of these which I didn't change at all:
def show
#user = User.find(params[:id])
#status_updates = #user.status_update
end
def destroy
#status_update.destroy
flash[:success] = "Status update deleted!"
redirect_to status_update_path(current_user.id)
end
def delete_all
#all_status_updates = current_user.status_update
#all_status_updates.delete_all
flash[:success] = "All status updates deleted!"
redirect_to status_update_path(current_user.id)
end
In my console I can look up this user and check out it's status updates, and the status updates are all there as I expect them to be. Which is a good thing.
But ActiveRecord can't find them all of a sudden??
Oh yea.. and here is my gem file relating to status_updates for the delete_all button
resources :status_updates do
collection do
delete :delete_all
end
end
The delete buttons look like this:
<%= link_to "delete", status_update, method: :delete %>
<%= link_to "Reset Log", delete_all_status_updates_path, method: :delete, data: { confirm: "Are you sure? Once this log is reset there is no going back!"}%>
I put those there to demonstrate their paths.
This should teach me to write my test and push all my work to git instead of running unsecured :/
Thanks!
Your first problem is that most likely you don't have javascript properly enabled. In Rails the delete method is done via javascript. Make sure you include all js files properly in your layout and application.js. You need to have either prototype or jquery.

Activeadmin - undefined method `batch_action'?

I'm trying to use activeadmin's batch_action so I can run actions on more than one record. However when trying to run my rails server I get the following error.
undefined method `batch_action' for #<ActiveAdmin::ResourceDSL:0xb11f980> (NoMethodError)
Here is the activeadmin resource code:
ActiveAdmin.register Product do
batch_action :destroy, false
filter :name
index do
selectable_column
column :name
default_actions
end
controller do
def current_company
#current_company
end
end
end
I'm not sure where I'm getting it wrong - I need to show a corresponding checkboxes against the records and then define a batch action. Where am I getting it wrong here?
Got the answer :) was a wrong entry in my gemfile.
https://github.com/gregbell/active_admin/issues/1302

rails3-amf - to_amf method not found on ActiveRecord objects

I am using the rails3-amf gem by warhammerkid in my Rails 3 / Flex 4 project.
AFAIK, I have correctly followed the "Getting Started" instructions from the GitHub page.
I have added the gem lines to my Gemfile.
I have installed the gems using bundle install.
From my Flex application, I will be making the RemoteObject call to the index action in the ManageMySchool::GradesController file. This is the code in the app/controllers/manage_my_school/grades_controller.rb file:
class ManageMySchool::GradesController < ApplicationController
respond_to :html, :amf
def index
#grade = Grade.first
respond_with(#grade) do |format|
format.amf { render :amf => #grade.to_amf }
end
end
end
The name of the model which is to be serialized is called Grade in both the Rails project (app/models/Grade.rb) and the Flex project (Grade.as with a RemoteAlias set as Grade). In the config/application.rb file, I have done the class mapping this way:
config.rails3amf.class_mapping do |m|
m.map :as => 'Grade', :ruby => 'Grade'
end
And I have done a parameter mapping this way:
config.rails3amf.map_params :controller => 'ManageMySchool::GradesController', :action => 'index', :params => [:authenticity_token]
Problem
Now, when I run the server and make the RemoteObject call from Flex, I get a to_amf undefined method error for the Grade model.
If I change Grade.first to Grade.all, #grade would have an array of Grades. But the undefined method error message still mentions the Grade model. This means that the to_amf method is working for the Array class but not for the ActiveRecord model.
Why is this? What am I doing wrong?
Is there something I have to do to "enable" the rails3-amf gem for ActiveRecord models?
I would appreciate any insights. Thanks!
Update
#warhammerkid: Here is the output of Grade.ancestors as seen in rails console.
ree-1.8.7-2011.03 :006 > puts Grade.ancestors
Grade
ActiveRecord::Base
Paperclip::CallbackCompatability::Rails3::Running
Paperclip::CallbackCompatability::Rails3
Paperclip::Glue CanCan::ModelAdditions
Authlogic::ActsAsAuthentic::ValidationsScope
Authlogic::ActsAsAuthentic::SingleAccessToken
Authlogic::ActsAsAuthentic::SessionMaintenance
Authlogic::ActsAsAuthentic::RestfulAuthentication::InstanceMethods
Authlogic::ActsAsAuthentic::RestfulAuthentication
Authlogic::ActsAsAuthentic::PersistenceToken
Authlogic::ActsAsAuthentic::PerishableToken
Authlogic::ActsAsAuthentic::Password
Authlogic::ActsAsAuthentic::MagicColumns
Authlogic::ActsAsAuthentic::Login
Authlogic::ActsAsAuthentic::LoggedInStatus
Authlogic::ActsAsAuthentic::Email
Authlogic::ActsAsAuthentic::Base
ActiveRecord::Aggregations
ActiveRecord::Transactions
ActiveRecord::Reflection
ActiveRecord::Serialization
ActiveModel::Serializers::Xml
ActiveModel::Serializers::JSON
ActiveModel::Serialization
ActiveRecord::AutosaveAssociation
ActiveRecord::NestedAttributes
ActiveRecord::Associations
ActiveRecord::AssociationPreload
ActiveRecord::NamedScope
ActiveModel::Validations::Callbacks
ActiveRecord::Callbacks
ActiveModel::Observing
ActiveRecord::Timestamp
ActiveModel::MassAssignmentSecurity
ActiveRecord::AttributeMethods::Dirty
ActiveModel::Dirty
ActiveRecord::AttributeMethods::TimeZoneConversion
ActiveRecord::AttributeMethods::PrimaryKey
ActiveRecord::AttributeMethods::Read
ActiveRecord::AttributeMethods::Write
ActiveRecord::AttributeMethods::BeforeTypeCast
#<Module:0x1028356f0> ActiveRecord::AttributeMethods::Query
ActiveRecord::AttributeMethods
ActiveModel::AttributeMethods
ActiveRecord::Locking::Optimistic
ActiveRecord::Locking::Pessimistic
ActiveRecord::Validations
ActiveModel::Validations::HelperMethods
ActiveModel::Validations
ActiveSupport::Callbacks
ActiveModel::Conversion
ActiveRecord::Persistence Object
PP::ObjectMixin Base64::Deprecated
Base64
ActiveSupport::Dependencies::Loadable
Kernel
Note that only ActiveModel::Serialization is there. No mention of Rails3AMF.
Does this mean I have to do something special to load the Rails3AMF module for the ActiveRecord models?
I am using Rails 3.0.5 with the latest version of ree. The gems are all contained in a gemset managed using rvm.
Update 2
If I remove the to_amf in the render :amf line, then I get the following error:
Grade Load (0.3ms) SELECT `grades`.* FROM `grades` LIMIT 1
Completed 200 OK in 195ms (Views: 0.1ms | ActiveRecord: 0.8ms)
Sending back AMF
NoMethodError (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.last):
Rendered /Users/anjan/.rvm/gems/ree-1.8.7-2011.03#rb/gems/actionpack-3.0.5/lib/> > action_dispatch/middleware/templates/rescues/_trace.erb (1.1ms)
Rendered /Users/anjan/.rvm/gems/ree-1.8.7-2011.03#rb/gems/actionpack-3.0.5/lib/> > action_dispatch/middleware/templates/rescues/_request_and_response.erb (2.8ms)
Rendered /Users/anjan/.rvm/gems/ree-1.8.7-2011.03#rb/gems/actionpack-3.0.5/lib/> > action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (13.6ms)
Started POST "/amf" for 127.0.0.1 at Fri Apr 15 17:03:34 +0530 2011
Sending back AMF
Update 3
If I manually add the line include Rails3AMF::Serialization at the top of the Grade.rb model file, then it all works. So, does this mean that I have to put this line in all my models?
I see that this is already being done in line numbers 40 - 42 in the lib/rails3-amf/serialization.rb file of the gem:
# Hook into any object that includes ActiveModel::Serialization
module ActiveModel::Serialization
include Rails3AMF::Serialization
end
Why isn't this working? Should I force-load the gem when my application initializes or something?
Thanks!
Update 4 - Solved by this workaround
Okay, I just ended up adding this code block in an initializer:
class ActiveRecord::Base
include Rails3AMF::Serialization
end
And it is working.
#warhammerkid - Thanks for the help.
Rails3AMF::Serialization, the module that adds the to_amf method, is included in ActiveModel::Serialization when Rails3-AMF loads. If it's somehow not being included even though the code is running and ActiveModel::Serialization is one of your model's ancestors, then the simplest solution is just to add "include Rails3AMF::Serialization" at the top of your model implementation. I've never tried gem sets before, but it might be an issue with them, as everything works correctly using Bundler.
As an aside, feel free to post a bug to https://github.com/warhammerkid/rails3-amf/issues.