ruby on rails - ActiveRecord complex SQL statement -
ruby on rails - ActiveRecord complex SQL statement -
can ar manage complex statements such following?
select count(p2.emp) indentation, p1.emp personnel p1, personnel p2 p1.lft between p2.lft , p2.rgt grouping p1.emp order p1.lft;
from: http://www.ibase.ru/devinfo/dbmstrees/sqltrees.html
my variation of sql is:
select count(r2.name) indentation, r1.name regions r1, regions r2 r1.lft between r2.lft , r2.rgt grouping r1.name order r1.lft
i have located next have no thought how link together.
.count(r2.name) .where(r1.lft: r2.lft..r2.rgt) .group("r1.name") .order("r1.lft")
yes. of sql keywords use, including implied bring together should documented in active record query interface guide. allow me know if find aren't.
for between
can utilize ruby's range
object, may want utilize explicit comparing operators. it's matter of personal style, think.
once you've learned how build these queries hand, check out gems awesome_nested_set can build them you.
sql ruby-on-rails
Comments
Post a Comment