retrieving table entries only from sql [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
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 %>

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.

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

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

How to calculate a record's ranking compared to others based on a single column 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
Assuming I have User model with a column called point.
How can I get an instance's ranking compared to all other User rows.
The user's rank can be thought of as the number other users with more points than them, plus 1. As Mischa pointed out in the comments, an appropriate place for this would be in the User model itself, as in:
def ranking
User.where('point > ?', point).count + 1
end
You would then call it as:
Rank: <%= #user.ranking %>
This has the downside of ranking users with the same number of points at the same rank. You'll have to decide on a "tiebreaker" for that case.

Resources