Rails 4 rewrite of join and group query [closed] - ruby-on-rails

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Here is my old query:
Dispenser.includes(:dispedus).includes(:educations).group('education.name')
To reiterate my post title please rewrite this to enable the same query in Rails 4.
Put in question form, how can this be rewritten to work in rails 4 ? ? ?

With multiple independent associations use commas:
Dispenser.joins(:dispedus, :educations).group('educations.name')
http://guides.rubyonrails.org/active_record_querying.html#joining-multiple-associations
With a nested join you can write:
Dispenser.joins(dispedus: :educations).group('educations.name')
http://guides.rubyonrails.org/active_record_querying.html#joining-nested-associations-single-level

Related

Ruby and Rail - How to combine date and time to get data [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
Currently, I have 3 columns : booking_date, booking_time_from, booking_time_to.
Problem is how can i get all data have booking time 2 hours ago using Active Support ?
Booking.where([booking_time], 2.hours.ago.to_datetime)
I'm not sure about what's the difference between booking_time_from and booking_time_to, and which column that the booking time you're referring here. Let's assume it's booking_time_to, you can
Booking.where("booking_time_to < ?", 2.hours.ago)

How to add a number to a current value in rails? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Sorry for my English. I have a model with some field.
How to add a number to a current value in field?
I want to add the number several times
i generated scaffold.I use this to create modal. But i want to add number to a current value when i clicked submit
#foo = Foo.first
#foo.bar += 10
#which is a shorter way of saying #foo.bar = #foo.bar + 10
#foo.save
If this isn't helpful, please add some more details to your question, eg your example code you have so far.

Ruby/Rails update model attribute with array items? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to populate column_name with items from #enc. Problem is #update_all updates all fields with last item in array?
Last item is "MPq3KSzDzLvTeYh+h00HD+5FAgKoNksykJhzROVZWbIJ36WNoBgkSoicJ5wx\nog0g\n".
I am trying to populate with all items from array not just last.
I hope question is clear?
I tried #update_attributes, but no success?
Help.
Thanks
#enc=["hUt7ocoih//kFpgEizBowBAdxqqbGV1jkKVipVJwJnPGoPtTN16ZAJvW9tsi\n3inn\n", "wGNyaoEZ09jSg+/IclWFGAXzwz5lXLxJTUKqCFIiOy3ZXRgdwFUsNf/75R2V\nZm83\n", "MPq3KSzDzLvTeYh+h00HD+5FAgKoNksykJhzROVZWbIJ36WNoBgkSoicJ5wx\nog0g\n"]
#enc.each do |i|
PaymentMethod.update_all(enc_number: i)
end
PaymentMethod.all.each_with_index do |payment, n = 0|
payment.update_column(:enc_number, #enc[n])
n +=1
end

retrieving table entries only from sql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi I have a option on my web app to show the last 5 submissions. But only by the Post ID. Not by every table within the post ID if that makes sense. So when someone clicks 'recent submissions' it shows them 5 Id numbers not every single table in the id.
Post.order('created_at DESC').limit(5)
if you require only the id of the last 5 elements, then do as below:
#first_five_posts = Post.order('created_at DESC').limit(5).pluck(:id)
VIEW
<%= #first_five_posts %>

Ruby sort an array in ascending order [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an array ['f_1', 'f_10', 'f_3', 'f_2']
I want to sort this array in ascending order using ruby one line code.
Do as below using sort_by :
['f_1', 'f_10', 'f_3', 'f_2'].sort_by { |s| s[/\d+/].to_i }
# => ["f_1", "f_2", "f_3", "f_10"]

Resources