2013-02-27 67 views
0

我想执行一个SQL查询,并将结果应用到我的能力类:选择查询到DB

res_sql = "SELECT * FROM people 
      where trainer_id != null and people.user_id = user.id 
      and user.role = 'Trainerone'" 

SQL后应当从典型Sportler返回一个对象,我可以将它应用于康康舞:

can :manage, **:sportler** 
+0

我无法准备我的querstion。所以:我有一个想法:也许这个,但它只适用于一个班级。 People.find_all_by_trainer_id_and_user_id(不为null,2)但与用户没有关系 – ubuseral 2013-02-27 21:10:48

+0

您错过了“用户”表的连接。 – 2013-02-27 21:14:10

+0

请停止向问题主题添加标签。 – 2013-03-03 02:22:36

回答

1

我不知道我理解你的问题,但也许这可以帮助你一点点:

People.joins(:trainer). 
     includes(:user). 
     where(user_id: user.id, user: { role: 'Trainerone' }) 

此代码implis如下:

  • 教练has_many人民
  • 人民belongs_to教练
  • 人民belongs_to用户
  • role是用户表的列 (在joinsincludes(等不同的关系,在where子句)可能需要使用复数,这取决于你的对象 - 关系)
+0

漂亮的导轨有他自己的语法。我做了这个myTrainer = People.joins ....然后我想在cancan中设置它:can:manage,myTrainer会这样工作吗? – ubuseral 2013-02-27 21:22:27

+0

你知道一件好事吗? – ubuseral 2013-02-28 02:58:21

+0

这里我猜:https://github.com/ryanb/cancan/wiki – MrYoshiji 2013-02-28 03:48:51