将友好的 id 与 includes 和 where 一起使用
using friendly id with includes and where
我使用这个查询来获取 属性 嵌套资源
@property=Property.includes(rooms: [:photos]).where(id: params[:id]).first
以上有效。但是现在我想用friendly_id。所以我尝试了 @property=Property.includes(rooms: [:photos]).friendly.where(id: params[:id]).first
但没有用。 friendly_id
的所有文档都使用 find
而不是 where
但在这里我怎样才能让它工作?
不可以,查看gem的源代码后发现friendly_id
gem并没有覆盖activerecord
的where
方法。
如果您使用 friendly_id
,为什么还要查询 id
?
您可以直接查询 slug 列。
您可以只过滤您用于 friendly_id 的列,通常命名为 slug
Property.includes(roomes: [:photos]).where(slug: params[:id]).first
我使用这个查询来获取 属性 嵌套资源
@property=Property.includes(rooms: [:photos]).where(id: params[:id]).first
以上有效。但是现在我想用friendly_id。所以我尝试了 @property=Property.includes(rooms: [:photos]).friendly.where(id: params[:id]).first
但没有用。 friendly_id
的所有文档都使用 find
而不是 where
但在这里我怎样才能让它工作?
不可以,查看gem的源代码后发现friendly_id
gem并没有覆盖activerecord
的where
方法。
如果您使用 friendly_id
,为什么还要查询 id
?
您可以直接查询 slug 列。
您可以只过滤您用于 friendly_id 的列,通常命名为 slug
Property.includes(roomes: [:photos]).where(slug: params[:id]).first