John
John

Reputation: 634

ActiveRecord connection select from a postgresql function

I am performing this query on my function:

ActiveRecord::Base.connection.execute("SELECT * FROM organic_reports('#{@date}');")

The function is returning the following attributes:

name(string), project_id(int), report_id(int), created_at(date), stats(int)

How can I use ActiveRecord to query those results, like in a normal Rails model? Methods like (where, find etc.)

Upvotes: 3

Views: 554

Answers (2)

infused
infused

Reputation: 24337

Why not just define a model for the organic_reports table?

class OrganicReport < ActiveRecord::Base
end

Now it is a Rails model which you can query normally:

OrganicReport.find_by(project_id: 1)
OrganicReport.where('name LIKE ?', "#{name}")

Upvotes: 0

Sean Hill
Sean Hill

Reputation: 15056

You can always use from to define the source of the query, and then you can use the ActiveRecord query interface as you normally would.

Model.from("organic_reports('#{@date}')").where('...').group('...')

Upvotes: 2

Related Questions