Maybe a question is little confusing but I will explain.
I wrote a query to select some data and I need to select title but title isn't column in table it is method in model (combined from two fields). How I can in query use that title method? I tried some things but it keeps telling me that title field is missing.
Concert.select('entity_changes.updated_at, concerts.id, concerts.title').joins('left join entity_changes on entity_changes.id = concerts.id')
.where(:clazz_name => 'Concert').order('entity_changes.updated_at DESC').limit(5)
and I get this error
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column "title" does not exist
Related
i've some query to select all data based on skipped_play_id but when i'm execute the query it still return some duplicate skipped_play_id
user = User.first
skipped_plays=user.user_skipped_plays.select(:skipped_play_id,:created_at).group(:created_at,:skipped_play_id)
and why should i provide :created_at as one of group by argument if i only need query grouped by skipped_play_id. if i change my query to user.user_skipped_plays.select(:skipped_play_id,:created_at).group(:skipped_play_id)
it will return
ActiveRecord::StatementInvalid: PG::GroupingError: ERROR: column "user_skipped_plays.created_at" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT "user_skipped_plays"."skipped_play_id", "user_skippe...
^
: SELECT "user_skipped_plays"."skipped_play_id", "user_skipped_plays"."created_at" FROM "user_skipped_plays" WHERE "user_skipped_plays"."user_id" = $1 GROUP BY "user_skipped_plays"."skipped_play_id" ORDER BY "user_skipped_plays"."id" ASC LIMIT $2
from /Users/fourtyonestudio/.rvm/gems/ruby-2.3.1/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/postgresql_adapter.rb:657:in `prepare
i'll really appreciate any advice
I think you don't need GROUP BY in your code at all. If you need to select unique rows containing just skipped_play_id and created_at you can go with:
user.user_skipped_plays.select(:skipped_play_id, :created_at).uniq
But in such selection you will still get duplicated skipped_play_id if you have different rows with same skipped_play_id, but different created_at, because you take a combination. So, possibly, you just need to go with:
user.user_skipped_plays.select(:skipped_play_id).uniq
It depends on what you need. You can type in comments for my answer how do you plan to use this selection and I will help you to solve your issue
Please try this
User.first.user_skipped_plays.group_by { |t| t.skipped_play_id }
Let me know if you are getting correct data or not
I have got difficulties group in PostGreSQL. My code below
Invoice.select("customer_id, due_date, sum(balance) AS total_balance, total_mount").group(:customer_id)
I have got the errors
Invoice Load (1.8ms) SELECT customer_id, due_date, sum(balance) AS total_balance, total_amount FROM "records" WHERE "records"."type" IN ('Invoice') GROUP BY "records"."customer_id"
ActiveRecord::StatementInvalid: PG::GroupingError: ERROR: column "records.due_date" must appear in the GROUP BY clause or be used in an aggregate function
enter code here
LINE 1: SELECT customer_id, due_date, sum(balance) AS total_balance,...
UPDATE
Thanks for #slicedpan for the solution. I have update like below code.
Invoice.select("customer_id, MAX(due_date), SUM(balance) AS total_balance").group(:customer_id)
I have read many of articles about that but It still show the error.
This error is due to the fact that Postgres doesn't know what to do with the due_date column. The query you have written is basically:
for each customer, show me the total sum of the balances for all their invoices, and the due date for one of their invoices.
The problem here is that in PostgreSQL you have to be explicit about which invoice you want to show the due_date of, whereas in MySQL or SQLite it will pick one (can't remember how off the top of my head). In this case I think it would make more sense to leave out the due_date from the select, otherwise use MAX(due_date) to get the most recent, or something like that.
You can get the same functionality as MySQL/SQLite if you do it like this:
Invoice.select("DISTINCT ON (customer_id) customer_id, due_date, sum(balance) over(partition by customer_id) AS total_balance, total_mount")
It will sum balance and select "random" due_date/total_mount for each customer_id. Just like non-standard GROUP BY possible in those 2 other RDBMS.
I'm having a problem with a .first query in Rails 4 ActiveRecord. New behavior in Rails 4 is to add an order by the id field so that all db systems will output the same order.
So this...
Foo.where(bar: baz).first
Will give the query...
select foos.* from foos order by foos.id asc limit 1
The problem I am having is my select contains two sum fields. With the order by id thrown in the query automatically, I'm getting an error that the id field must appear in the group by clause. The error is right, no need for the id field if I want the output to be the sum of these two fields.
Here is an example that is not working...
baz = Foo.find(77).fooviews.select("sum(number_of_foos) as total_number_of_foos, sum(number_of_bars) as total_number_of_bars").reorder('').first
Here is the error...
ActiveRecord::StatementInvalid: PG::GroupingError: ERROR: column "foos.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: ...bars FROM "fooviews" ORDER BY "...
Since the select is an aggregate expression, there is no need for the order by id, but AR is throwing it in automatically.
I found that I can add a reorder('') on to the end before the .first and that removes the order by id, but is that the right way to fix this?
Thank you
[UPDATE] What I neglected to mention is that I'm converting a large Rails 3 project to Rails 4. So the output from the Rails 3 is an AR object. If possible, the I would like the solution to keep in that format so that there is less code to change in the conversion.
You will want to use take:
The take method retrieves a record without any implicit ordering.
For example:
baz = Foo.find(77).fooviews.select("sum(number_of_foos) as total_number_of_foos, sum(number_of_bars) as total_number_of_bars").take
The commit message here indicates that this was a replacement for the old first behavior.
I'm trying to sort a list of items based on the value of field on a joined table. Here's what I've got so far:
FeaturedEvent.joins(:event).order('event.start_datetime').limit(5)
This looks right to me but when it's run it returns a Postgres error about there being a missing FROM statement:
PG::UndefinedTable: ERROR: missing FROM-clause entry for table "event" LINE 1: ...ts"."id" = "featured_events"."event_id" ORDER BY event.star... ^ : SELECT "featured_events".* FROM "featured_events" INNER JOIN "events" ON "events"."id" = "featured_events"."event_id" ORDER BY event.start_datetime LIMIT 5
I tried the suggestions in this post about putting the ordering in default scope but it came out with the same error – I'm guessing it's a Postgres thing.
How can I fix this?
Thanks!
Try
FeaturedEvent.includes(:event).order('events.start_datetime').limit(5)
In your order the table name should be the real database table name.
As I guess, the table name must be events
Your table name is events so you just have to add s in event
FeaturedEvent.joins(:event).order('events.start_datetime').limit(5)
In my application I use sqlite as a Backing store.For my use I create Two tables for Surgeon and Schedule with surgeon having columns Name(VARCHAR),id(int) and Schedule having id(int),Surgeon(int),Values(VARCHAR).
The Surgeon column in Schedule table is pointing to the id column in Surgeon table. I use The Below query to select values from the Schedule table for the Surgeon with id=1
SELECT Schedule.Values,Name from Schedule,Surgeon where Schedule.Surgeon==Surgeon.id and Surgeon.id=2
But I got error as below
SQLiteManager: Likely SQL syntax error: SELECT Schedule.Values,Name from Schedule,Surgeon where Schedule.Surgeon==Surgeon.id and Surgeon.id=2 [ near "Values": syntax error ]
I don't know where it went wrong, I have't used the database before so forgive me if the question is much basic
You have named one of the columns in Schedule as values Change it to something else that is not a key word for sqlite. you should not use key string to name the column First you have to take a look at the keys in sqlite
If u want to take the Name, then try Surgeon.Name