So for some odd reason I keep getting this error:
undefined method `>' for nil:NilClass
Rails.root: C:/Sites/booking_saloniser - Calendar
Application Trace | Framework Trace | Full Trace
app/models/user.rb:11:in `block in upcoming_appointments'
app/models/user.rb:11:in `upcoming_appointments'
app/controllers/appointments_controller.rb:8:in `index'
appointment controller
def index
#upcoming_appointments = current_user.upcoming_appointments
end
user model
def upcoming_appointments
appointments.order(appointment_time: :desc).select { |a| a.appointment_time > (DateTime.now) }
end
Any chance someone could help me get around this error?
It appears one or more of your appointment records has a nil appointment_time.
If this is not expected, you should add a validation on the model that it's not null, and fix your existing data.
Otherwise (and this works as quick fix), you can chain a query to not include records with a nil appointment time:
def upcoming_appointments
appointments
.order(appointment_time: :desc)
.where.not(appointment_time: nil)
.select { |a| a.appointment_time > (DateTime.now) }
end
If where.not isn't available (if you're using an older version of rails), you can also use where("appointment_time != null")
Related
I have a Project and a Meeting Model in my application.
Query in meetings controller is as follows:
all_meetings = Project.get_all_meetings #project
meetings = Kaminari.paginate_array(all_meetings).page(params[:page]).per params[:per_page]
render_collection meetings, Meeting
In project model:
def self.get_all_meetings project
active_meetings = project.meetings.without_deleted.order updated_at: :desc
cancelled_meetings = project.meetings.only_deleted.order updated_at: :desc
active_meetings + cancelled_meetings
end
I want to use search method on all_meetings variable like all_meetings.search but getting error as
NoMethodError (undefined method `search' for #<Array:0x00007f20b0b74238>
Did you mean? bsearch
each):
app/controllers/api/v1/pm/meetings_controller.rb:9:in `index'
app/controllers/api/v1/base_controller.rb:85:in `set_global'
app/controllers/api/v1/base_controller.rb:117:in `handle_exceptions'
How can I do this?
you have to convert this whole logic to elasticsearch functionalities. put pagination and "get_all_meetings" filtering mechanism to a searchkick where clause. you can add more data to elasticsearch indexes by overriding "serach_data" function.
I am trying to create a method that loops through some objects and if a certain attribute is true, adds to the cost of the lesson to the money received, but keep getting the error undefined method `+' for nil:NilClass.
Here is the piece of code (the line producing the error start #activity.update_attribute):
def show
#activity = Activity.find(params[:id])
#participants = Participant.all.where(:activity_id => #activity.id).map(&:user_id).uniq
#all_participants = Participant.all.where(:activity_id => #activity.id)
#all_participants.each do |a_participant|
if a_participant.paid
#activity.update_attribute(:money_received, #activity.money_received + #activity.cost)
end
end
#users = Array.new
#participants.each do |participant|
#users.push(User.find(participant))
end
end
And here is a screenshot from my database to show that neither is a null value:
Here is the error message that I get when running the application:
Any help would be greatly appreciated
Many thanks
I am struggling ordering my loop through my Devise Users by the upvotes on tools created by them.
Inside of that loop I get the value I would like to use to order the elments with:
user.tools.map(&:cached_votes_up).sum
That's my loop:
- #users.order(user.tools.map(&:cached_votes_up).sum).each do |user| #incorrect!
And my controller:
#users = User.all
Without order in my loop everything runs fine, with it this error appears:
NoMethodError in Users#index
Showing c:/Users/Jonas/gitapps/ocubit/app/views/users/index.html.haml where line #21 raised:
undefined method `tools' for #<Class:0xac2db78>
I am grateful for each help!
You can use joins
#users = User.joins(:tools).group("users.id").order("SUM(tools.cached_votes_up)")
On Rails 4. I'm getting some strange errors when I try to load index pages in Active Admin. They were all working fine before, but suddenly I started getting this message (for this example I loaded my Categories index but it is happening for most of them):
NoMethodError in Admin::Categories#index
Showing c:/Ruby200/lib/ruby/gems/2.0.0/bundler/gems/active_admin-3fb7f03335b1/app/views/active_admin/resource/index.html.arb where line #1 raised:
undefined method `validators_on' for Ransack::Search:Class
Extracted source (around line #1):
insert_tag renderer_for(:index)
Application Trace | Framework Trace | Full Trace
config/initializers/form_builder.rb:12:in `label'
I did a search for that method name and it returned only this:
In form_builder.rb
if object.class.validators_on(method).map(&:class).include? ActiveRecord::Validations::PresenceValidator
if options.class != Hash
options = {:class => "required"}
else
options[:class] = ((options[:class] || "") + " required").split(" ").uniq.join(" ")
end
end
I can view the dashboard and individual row pages fine, but when I go to edit a record I get this:
TypeError in Admin::Categories#edit
Showing c:/Ruby200/lib/ruby/gems/2.0.0/bundler/gems/active_admin-3fb7f03335b1/app/views/active_admin/resource/edit.html.arb where line #1 raised:
no implicit conversion of String into Array
Extracted source (around line #1):
insert_tag renderer_for(:edit)
Application Trace | Framework Trace | Full Trace
config/initializers/form_builder.rb:16:in `label'
I have no idea what this means...would it be better to re-install Active Admin/regenerate its assets? Is that a safe thing to do? If so, how do I do that? Or, is there a simple fix to these error messages. Thanks for any help.
I encountered the same issue and have a fix for you. The issue is that the very clever initializer that automatically adds a CSS * to required fields is not compatible with the Ransack search that ActiveAdmin uses. The solution is to check that the model responds_to the necessary method before invoking it:
class ActionView::Helpers::FormBuilder
# http://blog.pothoven.net/2012/10/self-marking-required-fields-in-rails.html
alias_method :orig_label, :label
# add a 'required' CSS class to the field label if the field is required
def label(method, content_or_options = nil, options = nil, &block)
if content_or_options && content_or_options.class == Hash
options = content_or_options
else
content = content_or_options
end
options = add_required_class(options) if presence_required?(method)
orig_label(method, content, options || {}, &block)
end
private
def add_required_class(options)
return { class: 'required' } unless options.class == Hash
new_class = ((options[:class].to_s || '') + ' required')
.split(' ').uniq.join(' ')
options.merge!(class: new_class)
end
def presence_required?(method)
object.class.respond_to?(:validators_on) &&
object.class.validators_on(method).collect(&:class)
.include?(ActiveRecord::Validations::PresenceValidator)
end
end
AA works with Rails 4 and Ruby 2.1.1, however, you have to take AA from the master branch at Github. Please note that AA switched from the "meta_search" gem to "ransack" which is not API-compatible - so some things are sure to break.
Maybe you use custom filters? I had your kind of errors due to custom filters based on scopes since "ransack" does not feature anything like search_method from "meta_search". Here's how I work around this, just in case:
https://github.com/activerecord-hackery/ransack/issues/345
https://github.com/activerecord-hackery/ransack/issues/288
I've tried the same scenario in rails console
But why am I getting...
undefined method each for #<ProjectProcurementManagementPlan:0x007ff9ecda2148>
In my model, I have a callback that will update the form immediately
after_update :check_app_on_update?
def check_app_on_update?
self.each do |ppmp|
ppmp_year = ppmp.year
get_app = AnnualProcurementPlan.where(year: ppmp_year)
get_id = get_app.map{|a| a.id }
get_id.each do |app_id|
update_attribute(:annual_procurement_plan_id, app_id)
end
end
end
But keeps on getting undefined method 'each'
But whenever I remove the self.each loop...
I get 'stack level too deep' type of error.
Any workarounds will be appreciated.
EDIT
Okay, now i realize that I should stick to my old approach. And that is without each
def check_app_on_update?
ppmp_year = self.year
get_app = AnnualProcurementPlan.where(year: ppmp_year)
get_id = get_app.map{|a| a.id }
get_id.each do |app_id|
# ppmp = ProjectProcurementManagementPlan.last
# ppmp.update_attribute(:annual_procurement_plan_id, app_id )
#idd = app_id
end
update_attribute(:annual_procurement_plan_id, #idd )
end
But i am getting stack level too deep error
I saw from the logs, that there's an endless loop.
(0.5ms) UPDATE "project_procurement_management_plans" SET "status" = 'Approved', "updated_at" = '2013-05-31 09:55:00.000000', "annual_procurement_plan_id" = 1 WHERE "project_procurement_management_plans"."id" = 19
AnnualProcurementPlan Load (0.5ms) SELECT "annual_procurement_plans".* FROM "annual_procurement_plans" WHERE "annual_procurement_plans"."year" = 2012
(0.2ms) ROLLBACK
Completed 500 Internal Server Error in 16737ms
SystemStackError - stack level too deep:
(gem) actionpack-3.2.11/lib/action_dispatch/middleware/reloader.rb:70:in `'
My guess is:
stack level too deep occurs because update_attribute() calls check_app_on_update? again that calls update_attribute() that calls check_app_on_update? again ...
Need more information to be more confident ...
because self means 1 object and there is no .each method for 1 object, .each is for collections.