Rails Translate SQL raw in ActiveRecord - ruby-on-rails

I have this SQL query
sql = "select mt1.* from mail_templates as mt1 join mail_templates as mt2 where mt1.template_id = mt2.id AND mt1.technical_name='#{name}' AND mt2.festival_id=#{festival}"
If I execute this query with ActiveRecord::Base.connection.execute(sql).first it returns an array and I need to have a MailTemplate ActiveRecord.
I don't know how to do it.
Thanx for your help

I found the solution by using
sql = "select mt1.* from mail_templates as mt1 join mail_templates as mt2 where mt1.template_id = mt2.id AND mt1.technical_name='#{name}' AND mt2.festival_id=#{festival}"
MailTemplate.find_by_sql(sql)

Related

trying to convert sql to linq sql

I'm trying to convert this SQL code to linq sql. But I don't understand even with the doc... someone can help me please ?
select prcleunique, LibelleProjet, from projet a
where eqcleunique in (select EqCleunique from Compo where uscleunique = '{0}')
and (a.socleunique in (select socleunique from utilisat where uscleunique = '{0}') or a.socleunique is null)
and a.archive = 2 order by LibelleProjet", idUtilisateur);
Those nested sql queries can be broken down nicely in Linq. Every time you have a select have a seperate linq query:
var clause1 = from row in _db.Compo where uscleunique == '{0}' select EqCleunique;
Then use the clauses in the last query
var result = from row in _db.project where clause1.Contains(row.eqcleunique) select row.whatever;
I hope this example is enough to get you started.

how to do this raw sql query using active record query?

Using active record query how do I do the following sql query?
sql = "select * from events
inner join places on events.place_id = places.id
inner join users on events.user_id = users.id"
where events.id = #{event_id}
I've tried
Event.joins(:user, :place).includes(:user, place).find(event_id)
This almost does what I want however does select events.* and not select .
I then tried
Event.joins(:user, :place).select('*.*')
This however returns an ActiveRelation object and I'm not sure how to get my results out of this.
I've also tried
Event.joins(:user, :place).find(event_id)
and this throws an error.
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR: syntax error at or near "."
Not sure what else to try. Is what I'm trying to do not possible?
Event.joins(:user, :place).where("events.id = ?", event_id).select("events.*, users.*, places.*")

write query in ruby from SQL query

How do I write the below query in ruby.
select count(*), project_types.project_type_name,company_id from project_type_stages_questions,project_types where (project_type_stage_id in
(select project_type_stages_id from project_type_stages where project_type_id in (select project_type_id from project_types))) group by project_type_stage_id,project_types.project_type_name,company_id
If you have a complex sql to do, and you're going to use its result, you can pass it to the db manager:
sql = "select count(*), project_types.project_type_name,company_id from project_type_stages_questions,project_types where (project_type_stage_id in (select project_type_stages_id from project_type_stages where project_type_id in (select project_type_id from project_types))) group by project_type_stage_id,project_types.project_type_name,company_id"
ActiveRecord::Base.connection.execute(sql)
The public api don't provide much information, but it's here.
If you need an ActiveRecord::Relation as result, them I suggest to use Arel.

Rails inner join two sql statements to get ActiveRecord::Relation

I have complex query that I want to join itself to do additional computations. In sql I can do
SELECT t1.*, t2.*
FROM (SQL) AS t1
INNER JOIN
(SQL) AS t2
ON t1.num = t2.num - 1
Suppose the SQL is the query I want to join.
How to do that in rails with ActiveRecord / arel (or something else) to get ActiveRecord::Relation and to be able to use where() on it.
If I do it with sql and execute/select_all I get PG:result or hash and can't use where memthod anymore.
First solution is interpolating other query using to_sql method:
Place.select("places.*, communes.*").joins("INNER JOIN (#{
Commune.where(:id => [1,2,3]).to_sql
}) as communes ON communes.id = places.commune_id")
Second solution is using Arel's merge method:
Place.select("places.*, communes.*")
.joins(:commune)
.merge(Commune.where(:id => [1,2,3]))

converting mysql query to redbean query

Can anyone help me convert this mysql query to redbean query please?
SELECT category,sum(price) from transaction GROUP BY category.
We can use like
$results = R::getAll( 'SELECT category,sum(price) from transaction GROUP BY category' );
// $results is multidimensional array
Here is reference link: http://www.redbeanphp.com/queries
You don't need to convert anything. You just write:
$transactions = R::get("SELECT category,sum(price) from transaction GROUP BY category");
A better conversion would be:
$sql = "SELECT category, sum(price) FROM transaction GROUP BY category";
$transactions = R::getAll($sql);

Resources