I have a mongoDB database with movie. Each document has a movie name, an ID, and tags. I want to display all the unique tag values in a dropdown list and to also be able to click on the tag and be able to show all the movies it is related to. To do that I need the ID associated with the movie.
I'm able to display all the unique movies using this: Movies.distinct(:Tags).each {|x| puts x }
I've tried Movies.distinct(:Tags).each {|x| puts x._id }, this came up with an error.
Now how do I extract the ID value from x?
EDIT:
The document is structured like so
[ _id: Integer, movie: String, tags: Array ]
UPDATE:
Maybe a simpler question... if i have Movies.any_of(:Tags => /comedy/i) how can I get the value of _id? Movies.any_of(:Tags => /comedy/i).only(:_id) returns a document that I'm not able to get id from individually.. it just sets the other fields to nil.
Ended up using .only(:id) and saving that to a variable then going through it with .each and pushing it to an empty array
Related
I am using a ActiveRecord::Base to get some values from database. The result of this query is below:
Result:
{"parent_id"=>"4", "id"=>"3"}
{"parent_id"=>"10", "id"=>"23"}
{"parent_id"=>"13", "id"=>"29"}
{"parent_id"=>"15", "id"=>"35"}
Now, I need to get a id from parent_id. Example, I want to find how is the id associate to parent_id.
It's possible to do it?
Update
I have several hashes from PG::Result. I need to use the first key (parent_id) to get the second key (id).
Example: I need to know how is the id from parent_id = 4. In this case, will be 3.
I think the I need something like this:
"4" => 3
"10" => 23
"13" => 29
I hope I understood your question correctly.
I'm assuming parent_id and id are fields that belong to some model and the result you posted is a result of a query to that model.
Say, you store the result of your query in a variable named res and the input value for parent_id is pid, then try the following. It should give you the id value corresponding to the parent_id submitted as input variable named pid.
res.find_by(parent_id: pid).id
Hope that helps.
I have a list of companies which im using while creating company reports as a select list.
I'm populating select list with database value with [KEY] => [Value] array types, which is working fine and in exchangeArray
\Zend\Json\Json::encode($morningreport->company)
this is how select list value is treated. Working fine.
Now the the value of company is stored ["2", "1"] in this format.
Is it zend defined way. If that so, how can I change it. Or any way for converting into string or array or so.
My requirement is to loop through this value and returned with the name based on this "2" and "1" IDS.
Note: 2 and 1 are the IDS of the comapnies. and select list was passed the array like :
'2' => 'companyA'.
'1' => 'companyB'
So finally found the answer,
\Zend\Json\Json::encode($morningreport->company)
encodes array into json format, so after fetching back this
$result = ["2"]
\Zend\Json\Json::encode($result);
decode jscon format and it will return back array.
I am using some custom queries in rails.
code snippet looks like
#time_spent = TimeEntry.find(:all,
:joins => "INNER JOIN sometable ON x = y",
:select =>"id, subject, spent_on")
now to get values I am using
#time_spent[index][:spent_on]
#time_spent[index][:subject]
what I want is to use index numbers in place of symbols. So that at run time I don't need to know the fields in the select clause.
for e.g. i want to do some thing similar to this
#time_spent[index][1]
#time_spent[index][2]
or
If I could get metadata of resultset i can use that information
Comments please?
When #time_spent is a collection of objects, this will get the attribute's value for the index specified for the first [0] item in that collection:
#time_spent[0].attributes.values[index]
So, for example, to get the 5th attribute's value for the 2nd object in the collection:
#time_spent[1].attributes.values[4]
to get field names from result set, use attributes.keys method
I have one array, which are IDs from one ActiveRecord table.
so I would like to sort that array based on last name which is associated with that ID...how can I do that?
To clarify:
array #students=[], inside are IDs and I would like to sort by Student.find(ID).last
Thank you.
Dorijan
Without fully understanding the question, if you're given a list of id's, you can sort by last_name when you're doing the query:
Student.where("id IN (?)", #students).order(:last_name)
Of course, this assumes that #students is an array of ids and nothing else.
Responding to your comment, I'm not sure why you'd need to do that, but if your #student array is just a list of ids ignorant of the Student model and its attributes, and you would still like to order that array, you can do this:
#students = Student.where("id IN (?)", #students).order(:last_name).collect(&:id)
This will return an array of ids sorted by last name. But again, I don't really know what you have going on behind the scenes, and I'm not sure what you're asking for.
Based on your comment, you want to do the following:
Take in a list of IDs of students as input
Return a list of IDs ordered by the student's last name in the database.
You should be able to do the following:
Student.where(:id => #ids).order(:last_name).map(&:id)
Breaking this down:
where(:id => #ids) only selects Students with an ID in the ID array.
order(:last_name) sorts the results by last name.
map(&:id) takes in an array of Students and returns just the ID column. Essentially, the method in brackets (which is a shortcut for calling id for each student) is called for each student found, and the return values are assembled into a new array (which will only contain the ids).
Some gotchas:
If an ID doesn't exist in the database, it will be excluded from the results - if your result array is smaller than your input array, you may be trying to access a record that no longer exists.
If the Students table has a lot of columns, you may want to consider calling select(:id) so that you don't pull every column of the Student records from the database.
Student.find( #students ).sort_by { |s| s.last_name }
in a scenario i want to retrieve the records with different values so i used distinct for that,
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 fetch the id of Book as well along with title_id
so, please advise me how to work on this
thanks
use grouping:
Book.where(:user_id => #user.id).grouped('title_id')
problem is that if you do grouping you can't have different book ids, they are all grouped into single row. You can use GROUP_CONCAT to workaround that:
Book...select('books.*, GROUP_CONCAT(id) as ids')
that way you'll have book ids attribute for every group