Rails choose different

in the script I want to get records with different values, so I used different for this,

Book.where (["user_id =?", @User_id]). select ('distinct title_id')

`this, only retrives the records like this [#<Book title_id: 30>, #<Book title_id: 31> ]` 

but I want to also get the book id along with title_id

so please tell me how to work on this

thanks

+4
source share
1 answer

use grouping:

 Book.where(:user_id => @user.id).grouped('title_id') 
Problem

is that if you group, you cannot have different book identifiers, they are all grouped into one line. You can use GROUP_CONCAT to work around:

 Book...select('books.*, GROUP_CONCAT(id) as ids') 

this way you will have the book ids attribute for each group

+11
source

All Articles