Rails 4 Error: NoMethodError - undefined method - ruby-on-rails

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)")

Related

Confirmation URL for Shopify RecurringApplicationCharge is nil

Im trying to add a recurring charge to my shopify app. I followed the Shopify-Tutorial but wrote it slightly different. My root route goes to:
root 'mycontroller#charging'
the controller action is:
def charging
if #shop_domain != #myshop
#shop_charging_status = #shop.charging
unless ShopifyAPI::RecurringApplicationCharge.current
recurring_charge = ShopifyAPI::RecurringApplicationCharge.new(
name: "My App",
price: "1.99",
return_url: "https:\/\/appurl\/activated",
trial_days: 7,
terms: "$1.99 per month")
if recurring_charge.save
#tokens[:confirmation_url] = recurring_charge.confirmation_url
#shop_charging_status = true
#shop.save
redirect recurring_charge.confirmation_url
end
end
else
redirect_to myindex_path
end
When I try to start the app, i get an error: NoMethodError (undefined method `[]=' for nil:NilClass). It concerns the #token line. This line already confused me when i wrote the code, because the variable #token is only used in this method. But nevertheless, why is it nil?
What am I missing?
When I try to start the app, i get an error: NoMethodError (undefined method `[]=' for nil:NilClass). It concerns the #token line.
I assume you mean #tokens?
I think you're missing the first part of the tutorial here in which they set #tokens = {} in the initialize method and then store the access tokens for each shop in there.

undefined method `>' for nil:NilClass Ruby on Rails (NoMethodError)

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")

undefined method `+' for nil:NilClass - Values are not null

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

Facing issue NoMethodError (undefined method `include' for "56":String):

I am facing issue while writing this code
def sim_state
sim_employees = SimEmployee.find(params[:id].include(:employees))
respond_to do |format|
format.js {
render :layout => false,
:locals => {
:sim_employees => sim_employee
}
}
end
end
and in my sim_states.js.erb
$('#simState').text('<%= sim_employee.employees.app_state%>');
So it gives me this error
NoMethodError (undefined method `include' for "56":String):
when i use it with includes then it gives
undefined method `includes'
Please guide me how to solve this.
The reason is simple
params[:id]
is return you a String value which is "56" and includes works with ActiveRecord::Relation and not string. So you are not able to fire the query that ways.
SimEmployee.find(params[:id])
This will return again a result and not relation. Try using
SimEmployee.where(id: params[:id]).includes(:employees).first
This should help you.
PS : Using includes for one record is same as firing two independent queries i.e.
#sim_employee = SimEmployee.find(params[:id])
#emplyess = #sim_employee.employees
There is a simple solution for this:
SimEmployee.includes(:employees).find params[:id]
SimEmployee.find(params[:id].include(:employees))
This line is causing the issue, you are calling include(:employees) on params[:id] which would be a number.
And you can't actually do .find(params[:id].include(:employees) because find method returns an instance of Model class, in this case, an instance of SimEmployee class, and you can't run method include on it.
Edit:
SimEmployee.includes(:employees).where()
You can pass a hash in where(). This works if your SimEmployee model has has_many relation to Employee model.

Show child/nested attributes in ActiveAdmin index view column

I'm trying to show the value of a nested attribute in ActiveAdmin.
My code is as follows:
index do
column :code
column 'Sales Agent' do |client|
client.sales_agent.agent_name
end
end
This gives me:
NoMethodError in Admin/client_branches#index
Showing /Users/constantlm/.rvm/gems/ruby-1.9.2-p290/gems/activeadmin-0.4.3/app/views/active_admin/resource/index.html.arb where line #1 raised:
undefined method `agent_name' for nil:NilClass
I don't understand why this doesn't work, because when I do the following:
index do
column :code
column 'Sales Agent' do |client|
raise client.sales_agent.agent_name.inspect
end
end
It outputs:
RuntimeError in Admin/client_branches#index
Showing /Users/constantlm/.rvm/gems/ruby-1.9.2-p290/gems/activeadmin-0.4.3/app/views/active_admin/resource/index.html.arb where line #1 raised:
"Peter John"
Which is what I would expect the first statement to do as well (referring to the "Peter John"), but it's not. What am I missing?
Clear your records and start with new records. Additionally try adding
client.sales_agent.agent_name if !client.sales_agent.nil?

Resources