Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rails Mysql按一个关联表进行排序_Mysql_Ruby On Rails - Fatal编程技术网

Rails Mysql按一个关联表进行排序

Rails Mysql按一个关联表进行排序,mysql,ruby-on-rails,Mysql,Ruby On Rails,我有3个有关系的模型: User (id, email) SellerProfile (user_id, name) BuyerProfile (user_id, name) 用户可能有一个关联的表。 我正在按名称对结果进行排序,但名称可以来自卖家档案或买家档案表格 我用两个条件子查询构建它,但我希望在一个请求中得到它 我试着玩连接,但它不起作用,因为它永远不知道用户是否关联了seller\u profile或buyer\u profile 有人试过类似的吗?使用 如果您面临这样的问题,我会重

我有3个有关系的模型:

User (id, email)
SellerProfile (user_id, name)
BuyerProfile (user_id, name)
用户可能有一个关联的表。 我正在按名称对结果进行排序,但名称可以来自
卖家档案
买家档案
表格

我用两个条件子查询构建它,但我希望在一个请求中得到它

我试着玩连接,但它不起作用,因为它永远不知道用户是否关联了
seller\u profile
buyer\u profile

有人试过类似的吗?

使用


如果您面临这样的问题,我会重新考虑您的数据库设计。你需要分开的桌子吗?您能否提取一些常用表(例如,
Profile
)?

您需要使用左联接。请阅读关于如何在mysql中使用它的教程。
User.left_joins(:seller_profile, :buyer_profile).order("COALESCE(seller_profiles.name, buyer_profiles.name)")