E
E
eldar_web2016-02-26 16:01:44
Ruby on Rails
eldar_web, 2016-02-26 16:01:44

How in Ruby on Rails can you determine which account is a record from the database?

For example, there is the last record in the table Table.last.
How to find out what number of element it is in the table?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Evgeny Tkachenko, 2016-02-26
@eldar_web

Based on the fact that when we use the .all method relative to the model and get an array, then:
For example, we are looking for users, your_item_id = Table.last.id, we got id=45.
Now let's remove all the id records of the table, create an empty array all_items_ids = [], then write a block like Table.all.each { |item| all_items_ids.push(item.id }
Now we get the index, your_item_index = all_items_ids.index{ |id| id == your_item_id }
I wrote the first thing that came to mind, don't blame me

T
TyzhSysAdmin, 2016-02-26
@POS_troi

Better describe the task, I feel you went the wrong way.
Are you doing pagination? :)

C
Coffin, 2016-03-01
@Coffin

Table.where("id <= ?", Table.last.id).count

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question