Ruby on rails 如何在Ruby中编写复杂查询
需要建议,如何用Ruby编写复杂的查询 PHP项目中的查询:Ruby on rails 如何在Ruby中编写复杂查询,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,需要建议,如何用Ruby编写复杂的查询 PHP项目中的查询: $get_trustee = db_query("SELECT t.trustee_name,t.secret_key,t.trustee_status,t.created,t.user_id,ui.image from trustees t left join users u on u.id = t.trustees_id
$get_trustee = db_query("SELECT t.trustee_name,t.secret_key,t.trustee_status,t.created,t.user_id,ui.image from trustees t
left join users u on u.id = t.trustees_id
left join user_info ui on ui.user_id = t.trustees_id
WHERE t.user_id='$user_id' AND trustee_status ='pending'
group by secret_key
ORDER BY t.created DESC")
我对Ruby的猜测是:
get_trustee = Trustee.find_by_sql('SELECT t.trustee_name, t.secret_key, t.trustee_status, t.created, t.user_id, ui.image FROM trustees t
LEFT JOIN users u ON u.id = t.trustees_id
LEFT JOIN user_info ui ON ui.user_id = t.trustees_id
WHERE t.user_id = ? AND
t.trustee_status = ?
GROUP BY secret_key
ORDER BY t.created DESC',
[user_id, 'pending'])
是的,
find\u by\u-sql
将起作用,您也可以尝试以下方法:
Trustee.connection.execute('...')
或对于一般查询:
ActiveRecord::Base.connection.execute('...')
是的,
find\u by\u-sql
将起作用,您也可以尝试以下方法:
Trustee.connection.execute('...')
或对于一般查询:
ActiveRecord::Base.connection.execute('...')
备选方案1(好的)
你是说带ActiveRecord的Ruby吗?您正在使用ActiveRecord和/或Rails吗#find_by_sql
是ActiveRecord中存在的一种方法。此外,在这个查询中似乎并不真正需要用户表,但可能您遗漏了什么?无论哪种方式,我都会将其包含在我的示例中。如果您没有正确设置关系,此查询将起作用:
users_trustees = Trustee.
select('trustees.*, ui.image').
joins('LEFT OUTER JOIN users u ON u.id = trustees.trustees_id').
joins('LEFT OUTER JOIN user_info ui ON ui.user_id = t.trustees_id').
where(user_id: user_id, trustee_status: 'pending').
order('t.created DESC')
此外,请注意此解决方案的几点:
users\u trusters.each{| u | u['image']}
id
,时间戳被调用created\u at
和updated\u at
class Trustee < ActiveRecord::Base
self.primary_key = 'trustees_id' # wouldn't be needed if the column was id
has_one :user
has_one :user_info
end
class User < ActiveRecord::Base
belongs_to :trustee, foreign_key: 'trustees_id' # relationship can also go the other way
end
class UserInfo < ActiveRecord::Base
self.table_name = 'user_info'
belongs_to :trustee
end
或者,如果要获取所有受信人的图像:
Trustee.all.collect { |t| t.user_info.try(:image) } # using a #try in case user_info is nil
备选方案3(最佳)
受托人似乎只是某种特殊情况下的用户。如果您不介意重新构造表以进一步简化,可以使用STI
这可能超出了这个问题的范围,所以我将把您链接到这个问题的文档:请参阅“单表继承”。另请参阅他们从Martin Fowler()链接到的文章
资源
选项1(正常)
你是说带ActiveRecord的Ruby吗?您正在使用ActiveRecord和/或Rails吗#find_by_sql
是ActiveRecord中存在的一种方法。此外,在这个查询中似乎并不真正需要用户表,但可能您遗漏了什么?无论哪种方式,我都会将其包含在我的示例中。如果您没有正确设置关系,此查询将起作用:
users_trustees = Trustee.
select('trustees.*, ui.image').
joins('LEFT OUTER JOIN users u ON u.id = trustees.trustees_id').
joins('LEFT OUTER JOIN user_info ui ON ui.user_id = t.trustees_id').
where(user_id: user_id, trustee_status: 'pending').
order('t.created DESC')
此外,请注意此解决方案的几点:
users\u trusters.each{| u | u['image']}
id
,时间戳被调用created\u at
和updated\u at
class Trustee < ActiveRecord::Base
self.primary_key = 'trustees_id' # wouldn't be needed if the column was id
has_one :user
has_one :user_info
end
class User < ActiveRecord::Base
belongs_to :trustee, foreign_key: 'trustees_id' # relationship can also go the other way
end
class UserInfo < ActiveRecord::Base
self.table_name = 'user_info'
belongs_to :trustee
end
或者,如果要获取所有受信人的图像:
Trustee.all.collect { |t| t.user_info.try(:image) } # using a #try in case user_info is nil
备选方案3(最佳)
受托人似乎只是某种特殊情况下的用户。如果您不介意重新构造表以进一步简化,可以使用STI
这可能超出了这个问题的范围,所以我将把您链接到这个问题的文档:请参阅“单表继承”。另请参阅他们从Martin Fowler()链接到的文章
资源