Rails 3.2 Sum Integers in View - ruby-on-rails-3

My view lists a set of expenses, I would like to sum the values in the current view. This is the code I am trying in my controller.
#project = Project.find(params[:project_id])
#expense_list = #project.expenses.find_all_by_user_id(current_user.id).sort_by(&:expense_date)
#expense_total = #expense_list.sum(:amount)
The error I get is:
undefined method `+' for #<Expense:0x007f907a2ba888>
Any advice? I feel like I'm doing something wrong.
Thanks!

Try this:
#expense_total = #project.expenses.where(:user_id => current_user.id).sum(:amount)

Related

Locate database entry based on ID from another database entry in rails

I've been digging around a little trying to figure out how I should locate the "tweet_id" in my #savedtweets table and then locate that same "tweet_id" in my #newtweets table from a controller, so far I'ved tried something like this;
CONTROLLER
#stweet = Savedtweet.find(params[:id])
#newtweet = Newtweet.where(:tweet_id => #stweet.tweet_id)
#newtweet.status = 'new'
#newtweet.save
Basically I need to change the string "saved" in my Newtweets table to "new" based on the current Savedtweet ID. I just can't figure it out. If I do the following in console;
#stweet = Savedtweet.first
#newtweet = Newtweet.where(:tweet_id => #stweet.tweet_id)
It finds the right one. I've got to be close just not there yet. :)
You could do:
Newtweet.find_by_tweet_id(#stweet.tweet_id).update_attribute(:status, 'new')
The reason your code isn't working is because Newtweet.where() returns an array of objects. It should be Newtweet.where().first, though Newtweet.find_by_tweet_id is the preferred method.

Add data from one table to another in ruby on rails

In my current project, I have to get some data in a column of one table and put them to the 2nd table. The first table data have been saved as hash as follows:
---
- werweqr
- test
- B1
- B2
- B3
- xvxczv
I write the following code in the migration file to add the data from the first table to the 2nd table. But the data are not sending from the first to second.
#scenario_response = ScenarioResponse.where("selected_barriers != ?", "");
#scenario_response.each do |p|
p.selected_barriers.each do |barrier|
Settings.test = barrier
# SelectedBarriers.create(:scenario_response_id => p.id, :barrier => barrier)
end
end
Can anyone please let me know if there's something wrong in my code.
If so how to fix it?
Thanks a lot
I don't think you need to call "each" on p.selected_barriers.Try removing the each and doing this:
Settings.test=p.selected_barriers.
I'm new to RoR too..According to me,the scenario_response is a collection that returns all instances that have the selected_barriers as "". Since you are doing an each on the collection, you will just have one selected_barriers item for each of them.
Please try this and let me know if I'm wrong.
Also you are not doing update_attributes.
Try doing Settings.update_attributes(params[:test]) after Settings.test = barrier .

Rails3 Kaminari undefined with .all

Hi
I wonder how to work around the problem I have with the pagination gem "Kaminari".
For what I've understood you cant paginate #user = User.all.page(5)?
But what if I have this code and want to paginate that, is it possible or do I need to change the code?
#price = Price.joins(:retailer, :retailer => :profile).
where(['product_id=? AND size_id=?', params[:prod_id], params[:si_id]]).
group(:retailer_id).order("SUM((prices.price * #{params[:amount].to_i}) + profiles.shippingCost)").all
The only thing I receive right now when applying.page(5) to that code is
undefined method `page' for #<Class:0x000001023c4558>
You don't need the .all because the joins call, along with where and group, is returning an array of objects for you that meet your criteria. Remove your .all and call page on the instance variable (which you might want to rename to #pages or something else plural).

Associated models and SUM query in Rails

I've got two Rails models, a Child and a Parent say.
I know that I can do this:
Child.sum(:income, :conditions => "parent_id = #{#parent_id}")
But I want to be able to do this:
Parent.children.sum(:income)
But this is giving me the wrong values if I try it. Is there a more concise way of writing
Child.sum(:income, :conditions => "parent_id = #{#parent_id}")
?
TIA
[ps: Rails 3 dev environment]
Sorry but I have just found out the answer to this. I needed to add to_a to the collection of Child objects, and call a proc, as so:
Parent.children.to_a.sum(&:income)
This works a charm.
Sorry for bumping up an old thread, but I think I found better(best?) solution. Below is code for my project that I ended up
self.purchases.where(:script_id => script_id, :approved => true).sum(:instances)
It produces one query that does exactly what I need
SELECT SUM("purchases"."instances") AS sum_id FROM "purchases" WHERE "purchases"."customer_id" = 1 AND "purchases"."script_id" = 1 AND "purchases"."approved" = 't'
I ran into an issue where the child was delegating to the parent and I needed to find a sum.
children.to_a.sum(:parent_income)
was giving me a major N+1 problem. The solution was to use:
children.joins(:parent).sum(:income)

Rails 3 where query

I am sending an array from jquery via the url requerts to the rails controller.
when I do this in my controller
log_array = (params[:log_ids])
logger.debug "This is the array #{log_array.to_a}"
I get this in my server log
This is the array 85,84,83,82
I am trying this query to get all the selected logs:
#logs = Log.where(['"logs"."id" IN (?)', log_array])
I get this on the server log
SELECT "logs".* FROM "logs" WHERE ("logs"."id" IN ('85,84,83,82'))
It sould be like this
SELECT "logs".* FROM "logs" WHERE ("logs"."id" IN (85,84,83,82))
It seems like it puts the arry in like a string.
Is there any way to make the sql right for an array?
You're making things too SQL-ish. Try this:
Log.find_all(params[:log_ids])
Or Log.where(:id => params[:log_ids]) if you want to use where() goodness.
Would use the .where listed below...check out the link to the rails 3.1 deprecations.
http://m.onkey.org/active-record-query-interface