I have a survey model that works like so:
ResponseSets have many Responses
Responses belong_to Answer
Answer model has a "value" column.
Given a ResponseSet, I'd like the sum of the Answers that are associated with each Response.
Ie, what I'd like to be able to do, (in imaginary code) is:
response_set.responses.answers.sum('value')
However, this obviously doesn't work, I need to build a query through response_set.responses, but I don't know how.
What's the SQL-fu way to tackle this in ActiveRecord?
After much trial and error I came up with this relatively simple solution, I hope this helps others in the future:
response_set.responses.joins(:answer).sum('answers.value')
To make this more convenient I just made this a method in the ResponseSet Model:
def total_value
self.responses.joins(:answer).sum('answers.value')
end
Well if you're using Rails 3.2 you can do something like:
response_set.responses.answers.pluck(:value).inject{|sum,x| sum + x }
Are the answers Integers, in the sense that you're looking to find ALL numeric answers associated with a response and literally add them all up? I think you could use map and inject for something like this, depending exactly on how your models/associations are set up..
response_set.responses.answers.map(:&value).inject(:+)
Can you post your models?
Related
First, thank for this great fusejs.io component, works like a charm.
There is one tweak though that I would like to see. Or, maybe, this is already possible, but I just dont know how to do it. Any help would be appreciated.
Say we have a JSON that combines both a list of Products and Categories of Product. I would like my users to be able to search for both. However, the need is to get the Products always above. In my example below, is there a way to weight the 'ObjectType' Product higher than the other one?
Thanks in advance
{"title":"Product A1","ObjectType":"Product","text":"","tags":"","} ,{"title":"Product A2","ObjectType":"Product","text":"","tags":""} ,{"title":"Product B1","ObjectType":"Product","text":"","tags":"",} ,{"title":"Category A2","ObjectType":"Category","text":"","tags":""}
Thanks in advance
Is it possible to only have NER tag a subset on entities. For example I may only need the date and money entities how could I accomplish that?
I’ve looked through the EntityRecognized documentation but didn’t see anything around removing entities.
It looks like it might be possible to accomplish this by retraining the NER tagger. (If you're interested in this route, check out this article that discusses your problem.)
But are you absolutely sure it's necessary though?
For instance, you could create a method that filters the results so that it only returns the entity types you are looking for.
def get_entities(doc):
for entity in doc.ents:
if entity.label_ in ["DATE","MONEY"]:
yield entity
else:
continue
Then instead of iterating over doc.ents, you can iterate over get_entities(doc) instead.
This seems like the easier way to me.
Suppose I have Posts and posts' Comments. I want to filter all the Posts that have more than 10 comments. I began writing something like Posts.includes(:comments).group("post.id").count("comments.id"), to obtain a hash of posts and their counts, and I can extract the information from there, but I want some one-line straightforward way to do that
Sure I can use some pure sql syntax statements, but I want it in a pure rails way. Any idea ?
Assuming the models are named in the more typical singular form of Post and Comment and have the usual association relationship, then the following should work:
Post.joins(:comments).group('posts.id').having('count(comments.id) > 10')
I am having trouble optimizing a large activerecord query. I need to include an associated model in my request but due to the size of the return set I only want to include a couple of the associated columns. For example I have:
Post.includes(:user).large_set
While I am looking for something like:
Post.includes(:user.name, :user.profile_pic).large_set
I need to actually use the name and profile pic attributes so Post.joins(:user) is not an option as far as I understand.
select is what you are looking for:
Post.select("posts.*, users.name, users.profile_pic").large_set
http://guides.rubyonrails.org/active_record_querying.html#selecting-specific-fields
You'll have to use join to accomplish what you want, as includes does not have this functionality. Or you could white your own includes method :-)
I'm working with 23 fields here, at last count. I've generally thrown my hands up with trying to count them after reducing from a 31-fielded table by using a foreign-key.
All the good links
Fundamental explanation of how to read and understand Slick's schema code provided by one very good Faiz.
On 22+ parameters...
Stefan Zeigar has been immensely helpful in the example code he's written in this discussion and also more directly linked to here on Github
The good Stefan Zeigar has also posted here on plain SQL queries
What this post is about
I think the above is enough to get me on my way to a working refactoring of my app so that CRUD is feasible. I'll update this question or ask new questions if something comes up and stagnates me. The thing is...
I miss using for comprehensions for querying. I'm talking about Slick's Query Templates
The problem I run into when I use a for comprehensions is that the table... will probably have
object Monsters extends Table[Int]("monster_table"){
// lots of column definitions
def * = id /* for a Table[Int] despite
having 21 other columns I'm not describing
in this projection/ColumnBase/??? */
}
and the * projection won't describe everything I want to return in a query.
The usual simple for comprehension Slick query template will look something like this:
def someQueryTemplate = for {
m <- Monsters
} yield m
and m will be an Int instead of the entire object I want because I declared the table to be a Table[Int] because I can't construct a mapped projection of 22 params because of all the code that needs to be generated for compiler support of class generation for each tuple and arbitrariness
So... in a nutshell:
Is there any way to use Query Templates in Slick with 22+ columns?
I stumbled on this question after answering a similar question a couple days ago. Here's a link to the question. slick error : type TupleXX is not a member of package scala (XX > 22)
To answer the question here, you are able to use nested tuples to solve the 22 column limit. When solving the problem myself, the following post was extremely helpful. https://groups.google.com/forum/#!msg/scalaquery/qjNW8P7VQJ8/ntqCkz0S4WIJ
Another option is to use the soon-to-be-released version of Slick which uses HLists to remove the column count limitation. This version of Slick can be pulled from the Slick master branch on Github. Kudos to cvogt for pointing out this option. https://github.com/slick/slick/blob/master/slick-testkit/src/main/scala/com/typesafe/slick/testkit/tests/MapperTest.scala#L249