Group_by in Rails [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
I want to show my posts posted according to days (dates). Can anyone help me on this I will be very grateful.
Def index
#users = User.all
end
I want my output according to date.
User can store post.title and post.content but i want all the posts according to dates as below
Example:
2nd December
post1
post2
post3
3rd December
post4
post5
4th December
no post

#grouped_posts = Post.group("date(created_at)")
or you can also do
Post.all.group_by {|post| post.created_at.to_date}
Documentation here

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)

Find the number of months between two dates in decimal [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 5 years ago.
Improve this question
I want to calculate the number of months between two dates. I want the result in decimal point. For example, if my date range is "2017-02-01" to "2017-03-1", then I am expecting the number of months to be something like 1.033 in decimal.
Please help.
You should be able to do something like this
#Mondel.time_diff_in_months("2017-03-01", "2017-02-01")
def time_diff_in_months(grater_date, lesser_date)
Time.at(grater_date.to_time - lesser_date.to_time).month # 1
end
I hope that this helps

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 %>

Resources