couldnot maintain the string format that is needed - ruby-on-rails

I want my result in this format
data: [{"label":"present","value":35},
{"label":"absent","value":50},
{"label":"leave","value":45},
{"label":"performance","value":67}]
I did try this code
data: <%= #orders.map { |d| {label: d.shipping, value: d.price}}.to_json.html_safe -%>
which gives result
data: [{"label":"present","value":"35"},
{"label":"absent","value":"50"},
{"label":"leave","value":"45"},
{"label":"performance","value":"67"}]
here problem is in the first label present i obtain value "35" in this format but i need value 35 only without "". How could i get this?
my data in #orders is
[#<Order id: 8, price: "35", purchased_at: "2012-01"
, shipping: "present", created_at: "2012-12-13 08:17:39",
updated_at: "2012-12-13 08:50:13">, #<Order id: 9, price
: "50", purchased_at: "2012-02", shipping: "absent", created_at
: "2012-12-13 08:31:24", updated_at: "2012-12-13 08:50:55">,
#<Order id: 12, price: "45", purchased_at: "2012-03", shipping:
"leave", created_at: "2012-12-13 08:48:37", updated_at: "2012-12-13 08:51:07">
, #<Order id: 13, price: "67", purchased_at: "2012-04", shipping:
"performance", created_at: "2012-12-13 08:48:47", updated_at: "2012-12-13 08:51:18">]

data: <%= #orders.map { |d| {label: d.shipping, value: d.price.to_i}}.to_json.html_safe -%>

Related

Ruby loop over the dates

Hello I have Schedules table like this;
<ActiveRecord::Relation [#<Schedule id: 427, date: "2016-06-15", notes: nil, price: "350", promo: "", status: "available", boat_id: 45, created_at: "2016-06-09 19:43:15", updated_at: "2016-06-09 19:43:15">, #<Schedule id: 428, date: "2016-06-16", notes: nil, price: "350", promo: "", status: "available", boat_id: 45, created_at: "2016-06-09 19:43:15", updated_at: "2016-06-09 19:43:15">, #<Schedule id: 429, date: "2016-06-17", notes: nil, price: "350", promo: "", status: "available", boat_id: 45, created_at: "2016-06-09 19:43:15", updated_at: "2016-06-09 19:43:15">, #<Schedule id: 430, date: "2016-06-22", notes: "", price: "253", promo: "", status: "available", boat_id: 3, created_at: "2016-06-14 09:23:55", updated_at: "2016-06-14 09:23:55">, #<Schedule id: 431, date: "2016-06-23", notes: "", price: "253", promo: "", status: "available", boat_id: 3, created_at: "2016-06-14 09:23:55", updated_at: "2016-06-14 09:23:55">]
and user inputs "from" and "to" dates. Lets say I have variables such
a = "2016-06-16".in_time_zone('UTC')
b = "2016-06-19".in_time_zone('UTC')
I would like to loop over Schedule table between these dates (a and b) and get the sum of prices..
I think of a such solution that, I can say;
a = "2016-06-16".in_time_zone('UTC')
b = "2016-06-19".in_time_zone('UTC')
schedules = Schedule.all
price = 0
dates = []
while a < b do
dates << a
a += 1.day
end
Then;
dates.each do |date|
schedules.each do |schedule|
if (date == schedule.in_time_zone('UTC'))
price += schedule.price.to_i
end
end
end
but do not know if that is the most convenient way to do.
Why not taking the advantage of DB native implementations. You can do it in SQL.
date_from = "2016-06-16".in_time_zone('UTC')
date_to = "2016-06-19".in_time_zone('UTC')
Schedule
.where(date: date_from..date_to) # sql BETWEEN operator
.sum("price") # SQL aggregate function.

Join array and hash

Ruby on Rails 4
I am trying to join an Array of questions and a grouped by hash together by the question's id.
The Hash is grouped by the question_id attribute of its records from Answers.
The Array has all the Questions, and index is id.
I was trying to put them into an array, #pdf. The question first then the answers by the enumerator index. But it is behaving odd.
#pdf = []
#test = Test.find(params[:id])
#test_questions = #test.questions
answers = Answer.find(:all)
#all_answers = answers.group_by(&:question_id)
#test_questions.each do |q|
#pdf << q
#pdf << #all_answers.select { |i| i == q }
end
The log shows this:
=> [#<Question id: 1, content: "How did the chicken cross the road?", question_type: "MC", category: "ip_voice", product_id: 8, active: true, created_at: "2014-05-07 17:10:14", updated_at: "2014-05-07 17:10:14", user_id: 1>, {}, #<Question id: 2, content: "Is this working?", question_type: "TF", category: "ip_voice", product_id: 6, active: true, created_at: "2014-05-13 16:10:53", updated_at: "2014-05-13 16:10:53", user_id: 1>, {}]
This is #all_answers:
=> {1=>[#<Answer id: 1, content: "It walked", question_id: 1, correct: false, created_at: "2014-05-07 17:10:14", updated_at: "2014-05-07 17:10:14">, #<Answer id: 2, content: "It was thrown", question_id: 1, correct: true, created_at: "2014-05-07 17:10:14", updated_at: "2014-05-07 17:10:14">, #<Answer id: 3, content: "It got run over and pushed", question_id: 1, correct: false, created_at: "2014-05-07 17:10:14", updated_at: "2014-05-07 17:10:14">], 2=>[#<Answer id: 4, content: "False", question_id: 2, correct: true, created_at: "2014-05-13 16:10:53", updated_at: "2014-05-13 16:10:53">, #<Answer id: 5, content: "True", question_id: 2, correct: false, created_at: "2014-05-13 16:10:53", updated_at: "2014-05-13 16:10:53">]}
This is #test_questions:
=> #<ActiveRecord::Associations::CollectionProxy [#<Question id: 1, content: "How did the chicken cross the road?", question_type: "MC", category: "ip_voice", product_id: 8, active: true, created_at: "2014-05-07 17:10:14", updated_at: "2014-05-07 17:10:14", user_id: 1>, #<Question id: 2, content: "Is this working?", question_type: "TF", category: "ip_voice", product_id: 6, active: true, created_at: "2014-05-13 16:10:53", updated_at: "2014-05-13 16:10:53", user_id: 1>]>
I am new to many Rails/Ruby methods.
I think this is the code you need:
#pdf = []
#test = Test.find(params[:id])
#test_questions = #test.questions
answers = Answer.find(:all)
#all_answers = answers.group_by(&:question_id)
#test_questions.each do |q|
#pdf << q
#pdf += #all_answers[q.id]
end
This should create something like:
=> [#<Question id: 1, content: "How did the chicken cross the road?", question_type: "MC", category: "ip_voice", product_id: 8, active: true, created_at: "2014-05-07 17:10:14", updated_at: "2014-05-07 17:10:14", user_id: 1>, #<Answer id: 1, content: "It walked", question_id: 1, correct: false, created_at: "2014-05-07 17:10:14", updated_at: "2014-05-07 17:10:14">, #<Answer id: 2, content: "It was thrown", question_id: 1, correct: true, created_at: "2014-05-07 17:10:14", updated_at: "2014-05-07 17:10:14">, #<Answer id: 3, content: "It got run over and pushed", question_id: 1, correct: false, created_at: "2014-05-07 17:10:14", updated_at: "2014-05-07 17:10:14">,
#<Question id: 2, content: "Is this working?", question_type: "TF", category: "ip_voice", product_id: 6, active: true, created_at: "2014-05-13 16:10:53", updated_at: "2014-05-13 16:10:53", user_id: 1>, #<Answer id: 4, content: "False", question_id: 2, correct: true, created_at: "2014-05-13 16:10:53", updated_at: "2014-05-13 16:10:53">, #<Answer id: 5, content: "True", question_id: 2, correct: false, created_at: "2014-05-13 16:10:53", updated_at: "2014-05-13 16:10:53">]
What you probably want to do is to have something like this:
class Test < ActiveRecord::Base
has_many :questions
has_many :answers, :through => :questions
end
class Question < ActiveRecord::Base
belongs_to :test
has_many :answers
end
class Answer < ActiveRecord::Base
belongs_to :question
end
And then do:
test = Test.find(params[:id]).joins(:questions, :answers)
This will fetch questions and answers in one query and you can do things like:
test.questions.first.answers
or
test.answers
and for the pdf:
test.questions.each do |q|
pdf = test.questions.collect do |q|
[q, q.answers]
end

how to select data from database based created_at today in rails

how to select data from database based created_at today in rails?
example :
[#<Event id: 1, customer_id: 1, therapist_id: 1, location_id: 1, service_id: 3, walkin: true, cancel: false, cancel_reason: nil, room_id: 5, starts_at: "2014-04-03 21:31:13", stops_at: nil, created_at: "2014-04-14 10:13:52", updated_at: "2014-04-14 10:13:52">, #<Event id: 2, customer_id: 2, therapist_id: 2, location_id: 2, service_id: 2, walkin: false, cancel: false, cancel_reason: nil, room_id: 1, starts_at: "2014-04-05 21:31:13", stops_at: nil, created_at: "2014-04-14 10:13:52", updated_at: "2014-04-14 10:13:52">, #<Event id: 3, customer_id: 1, therapist_id: 2, location_id: 1, service_id: 1, walkin: false, cancel: false, cancel_reason: nil, room_id: 5, starts_at: "2014-04-07 21:31:13", stops_at: nil, created_at: "2014-04-14 10:13:52", updated_at: "2014-04-14 10:13:52">, #<Event id: 4, customer_id: 2, therapist_id: 2, location_id: 2, service_id: 2, walkin: false, cancel: false, cancel_reason: nil, room_id: 1, starts_at: "2014-04-09 21:31:13", stops_at: nil, created_at: "2014-04-14 10:13:52", updated_at: "2014-04-14 10:13:52">, #<Event id: 5, customer_id: 14, therapist_id: 2, location_id: 2, service_id: 1, walkin: true, cancel: false, cancel_reason: "", room_id: 8, starts_at: "2014-04-15 06:47:00", stops_at: "2014-04-15 07:47:00", created_at: "2014-04-15 06:47:14", updated_at: "2014-04-15 06:47:14">]
and when i collect data based created_at today i get result :
<Event id: 5, customer_id: 14, therapist_id: 2, location_id: 2, service_id: 1, walkin: true, cancel: false, cancel_reason: "", room_id: 8, starts_at: "2014-04-15 06:47:00", stops_at: "2014-04-15 07:47:00", created_at: "2014-04-15 06:47:14", updated_at: "2014-04-15 06:47:14">
how to collect all data where data created_at just today in rails?
thanks before
Please try this:
Event.where("DATE(created_at) = DATE(?)", Time.now)
Please have a try with this code.
Event.where("created_at >= ? and created_at <= ?", Time.now.beginning_of_day, Time.now.end_of_day)
To fetch today's events, it will return only today's events
Event.where("created_at = ?", Date.today)
Here's what I have tried in my console
2.0.0p247 :008 > Article.where("created_at < ?", Date.today)
Article Load (0.5ms) SELECT `articles`.* FROM `articles` WHERE (created_at < '2014-04-15')
=> #<ActiveRecord::Relation [#<Article id: 2, title: "Villa Kerylos: A Greek Dream.. A Modern Villa", description: "An inside look at one of the Mediterranean's most i...", url: "http://frenchantiques.blogspot.com/2009/05/villa-ke...", is_open_in_new_window: true, created_at: "2013-12-26 07:03:17", updated_at: "2013-12-26 10:28:36">, #<Article id: 4, title: "Kenzo Auctions Personal Art Collection", description: "Kenzo Auctions Personal Art Collection", url: "http://frenchantiques.blogspot.com/2009/06/kenzo-au...", is_open_in_new_window: false, created_at: "2013-12-26 09:57:21", updated_at: "2013-12-26 09:57:21">]>
Event.where("created_at > ?" Time.now.to_date.to_time) should work, because you want todays entries. Also, a good Idea, is those of the last 24 hours, which should be Event.where("created_at > ?-60*60*24 and created_at < ?" Time.now)
Event.where('created_at >= ?', Date.today)
Maybe you can parse the created_at to the same short format and compare, but this should be enough.

Trying to deleting from Active Record collection ONLY using delete_if

I want to remove elements from an Active Record collection, for this I'm trying to use delete_if. Below is my collection:
categories = Category.all
groups = Group.all
result = []
groups.each do |g|
result.push({ :name => g[:name], :amount => g[amount_column], :drilldown => [] })
categories.where( { :group_id => g.id } ).each do |c, index|
result.last[:drilldown].push({ :name => c[:name], :amount => c[amount_column], :drilldown => nil })
end
# lastly, remove these categories from this collection (not working)
categories.delete_if {|c| c.group_id == g.id }
# tried this too - categories.select! { |c| c.group_id != g.id }
end
abort(categories.size.inspect)
..I want to do something with the categories of each group, then remove them form the array. After the loop block I'll want to be left with the categories which do not belong to a group - that is why I delete those of each group there. Here is the data for groups and categories when they are first fetched (sorry it's a lot but just to note that some category.group_id and group.id match:
#categories
#<ActiveRecord::Associations::CollectionProxy [#<Category id: 13, name: "Internet", user_id: 1, created_at: "2014-03-09 13:15:19", updated_at: "2014-03-15 05:13:47", amount: #<BigDecimal:7f0c283806e0,'0.0',9(18)>, group_id: nil, amount_in: #<BigDecimal:7f0c28380578,'0.0',9(18)>, amount_out: #<BigDecimal:7f0c283804b0,'0.0',9(18)>>, #<Category id: 15, name: "Electricity", user_id: 1, created_at: "2014-03-09 13:15:39", updated_at: "2014-03-15 05:14:03", amount: #<BigDecimal:7f0c2838f5a0,'0.0',9(18)>, group_id: nil, amount_in: #<BigDecimal:7f0c2838f460,'0.0',9(18)>, amount_out: #<BigDecimal:7f0c2838f2f8,'0.0',9(18)>>, #<Category id: 6, name: "Phone", user_id: 1, created_at: "2014-02-18 10:07:47", updated_at: "2014-03-15 05:16:21", amount: #<BigDecimal:7f0c2838e470,'0.0',9(18)>, group_id: nil, amount_in: #<BigDecimal:7f0c2838e308,'0.0',9(18)>, amount_out: #<BigDecimal:7f0c2838e240,'0.0',9(18)>>, #<Category id: 9, name: "Trains", user_id: 1, created_at: "2014-02-22 03:08:08", updated_at: "2014-03-15 05:16:21", amount: #<BigDecimal:7f0c2838d340,'-0.5E3',9(18)>, group_id: nil, amount_in: #<BigDecimal:7f0c2838d138,'0.0',9(18)>, amount_out: #<BigDecimal:7f0c2838cf08,'-0.5E3',9(18)>>, #<Category id: 5, name: "Resaurants", user_id: 1, created_at: "2014-02-17 12:12:44", updated_at: "2014-03-15 05:16:21", amount: #<BigDecimal:7f0c2839be18,'-0.235E3',9(18)>, group_id: 16, amount_in: #<BigDecimal:7f0c2839bc38,'0.0',9(18)>, amount_out: #<BigDecimal:7f0c2839baa8,'-0.235E3',9(18)>>, #<Category id: 14, name: "Gas", user_id: 1, created_at: "2014-03-09 13:15:27", updated_at: "2014-03-15 05:16:21", amount: #<BigDecimal:7f0c2839a680,'0.0',9(18)>, group_id: 16, amount_in: #<BigDecimal:7f0c2839a3d8,'0.0',9(18)>, amount_out: #<BigDecimal:7f0c2839a1d0,'0.0',9(18)>>, #<Category id: 3, name: "Snowboarding", user_id: 1, created_at: "2014-02-11 09:43:52", updated_at: "2014-03-15 05:16:21", amount: #<BigDecimal:7f0c28398d30,'-0.1111E4',9(18)>, group_id: 16, amount_in: #<BigDecimal:7f0c28398b28,'0.0',9(18)>, amount_out: #<BigDecimal:7f0c28398a38,'-0.1111E4',9(18)>>]>
# groups
#<ActiveRecord::Associations::CollectionProxy [#<Group id: 16, name: "Testing", user_id: 1, amount: #<BigDecimal:31c0ce8,'-0.1346E4',9(18)>, created_at: "2014-03-09 13:48:50", updated_at: "2014-03-15 05:35:57", amount_in: #<BigDecimal:31d3118,'0.0',9(18)>, amount_out: #<BigDecimal:26b9728,'-0.1346E4',9(18)>>, #<Group id: 17, name: "test", user_id: 1, amount: #<BigDecimal:31d2a88,'0.0',9(18)>, created_at: "2014-03-15 05:38:36", updated_at: "2014-03-15 05:39:00", amount_in: #<BigDecimal:31d14f8,'0.0',9(18)>, amount_out: #<BigDecimal:2e4e650,'0.0',9(18)>>]>
.. but my delete_if does nothing (abort shows the same size with or without the delete_if). Please note that there are group.id = 16, and category.group_id = 16 so the delete_if should delete those ones - but it doesn't. Is there anything that I'm doing wrong?
Try this ,I think it will help
categories.delete_if {|x| x.group_id==16}
This will update categories in place, selecting all categories with group_id not equal to g.id.
categories.select! { |c| c.group_id != g.id }

How to get an array of attributes hashes from an array of records?

I want to print all questions and status fields.
How can I extract the required data in an array ?
This is my array : #category_questions
[#<Question id: 38, user_id: 1, question: "hi", question_status: 1, created_at: "2013-06-04 18:32:28", updated_at: "2013-06-04 18:32:28">, #<Question id: 40, user_id: 1, question: "urll", question_status: 1, created_at: "2013-06-04 18:34:57", updated_at: "2013-06-04 18:34:57">, #<Question id: 41, user_id: 1, question: "urll", question_status: 1, created_at: "2013-06-04 18:35:31", updated_at: "2013-06-04 18:35:31">]
I tried #category_questions[iteration_number][:some field] but it did not work.
To get an array of hashes:
#category_questions.map do |question|
{question: question.question, status: question.question_status}
end
To get an array of arrays:
#category_questions.map {|question| [question.question, question.question_status] }

Resources