Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.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
Ruby on rails 友好的\u id为Rails中的多个对象创建重复的slug_Ruby On Rails_Ruby On Rails 4_Friendly Id - Fatal编程技术网

Ruby on rails 友好的\u id为Rails中的多个对象创建重复的slug

Ruby on rails 友好的\u id为Rails中的多个对象创建重复的slug,ruby-on-rails,ruby-on-rails-4,friendly-id,Ruby On Rails,Ruby On Rails 4,Friendly Id,我的Rails构建不包含路径名。该应用程序由两个主要对象组成:集合和项。因此,如果我有一个链接:,这可能会识别一个集合或一个项目。在整个应用程序的上下文和UI流中,差别是显而易见的 我的问题是:有没有一种方法可以让友好的ID宝石通过查看一个蛞蝓是否已经被另一个物体捕获来生成唯一的蛞蝓?我知道您可以生成候选项,以便友好ID不会复制给定对象的slug,但我需要友好ID在生成新slug之前检查现有集合slug和项目slug 我希望这听起来不要太混乱。更简洁地说:友好ID是否有一种方法可以在生成新的sl

我的Rails构建不包含路径名。该应用程序由两个主要对象组成:
集合
。因此,如果我有一个链接:,这可能会识别一个集合或一个项目。在整个应用程序的上下文和UI流中,差别是显而易见的

我的问题是:有没有一种方法可以让友好的ID宝石通过查看一个蛞蝓是否已经被另一个物体捕获来生成唯一的蛞蝓?我知道您可以生成候选项,以便友好ID不会复制给定对象的slug,但我需要友好ID在生成新slug之前检查现有集合slug和项目slug


我希望这听起来不要太混乱。更简洁地说:友好ID是否有一种方法可以在生成新的slug之前检查多个对象中的slug?

注意这都是未经测试的,只是从文档开始工作并阅读源代码

您可以继承FriendlyId::SlugGenerator类,并重写available?方法以检查对方模型中的现有记录:

class CrossModelSlugGenerator << FriendlyId::SlugGenerator

    def available?(slug)
      if (@scope.class == "Item::ActiveRecord_Relation")
         # Search for collections with this slug and return false if they exist.
      elsif (@scope.class == "Collection::ActiveRecord_Relation")
         # Search for items with the this slug and return false if they exist.
      end

      # Otherwise do a normal slug check
      !@scope.exists_by_friendly_id?(slug)
    end

end
尝试一下,看看它是否适合你。再说一次,我还没有测试过任何一个,但它似乎应该是可行的

编辑-您可能需要将类包装在FriendlyId模块中,如下所示:

include "friendly_id"

module FriendlyId
  class CrossModelSlugGenerator << SlugGenerator
    ...
  end
end
您可能需要包含在某个地方,可能在类定义中。另外,尝试将类包装到FriendlyId模块中,因此可能是这样的:

include "friendly_id"

module FriendlyId
  class CrossModelSlugGenerator << SlugGenerator
    ...
  end
end

注意这些都是未经测试的,只是从文档开始工作并阅读源代码

您可以继承FriendlyId::SlugGenerator类,并重写available?方法以检查对方模型中的现有记录:

class CrossModelSlugGenerator << FriendlyId::SlugGenerator

    def available?(slug)
      if (@scope.class == "Item::ActiveRecord_Relation")
         # Search for collections with this slug and return false if they exist.
      elsif (@scope.class == "Collection::ActiveRecord_Relation")
         # Search for items with the this slug and return false if they exist.
      end

      # Otherwise do a normal slug check
      !@scope.exists_by_friendly_id?(slug)
    end

end
尝试一下,看看它是否适合你。再说一次,我还没有测试过任何一个,但它似乎应该是可行的

编辑-您可能需要将类包装在FriendlyId模块中,如下所示:

include "friendly_id"

module FriendlyId
  class CrossModelSlugGenerator << SlugGenerator
    ...
  end
end
您可能需要包含在某个地方,可能在类定义中。另外,尝试将类包装到FriendlyId模块中,因此可能是这样的:

include "friendly_id"

module FriendlyId
  class CrossModelSlugGenerator << SlugGenerator
    ...
  end
end

这看起来很有希望,我明天早上第一件事就是检查一下,然后回复你。谢谢你,乔科!!不幸的是,我的应用程序无法找到“slug\u生成器\u类”。我不知道为什么会出现这个问题,我使用的是5.1.0版的gem,在我的模型中确实使用了“slugged”:
扩展FriendlyId friendly\u id:sluge\u候选对象,使用:[:slugged,:finders]
更新了原始答案,使用了一些不同的方法,看看它们是否适合你。你真棒!看起来有几种方法可以做到这一点,但您的方法非常有效。我还尝试添加各种slug候选项,如果条件允许,请在为给定集合选择新slug之前先查看我的项目标题。非常感谢您的明智和有益的回答<代码>slug\u generator\u类抛出
未定义方法
,并使用
CrossModelSlugGenerator
抛出
未定义方法CrossModelSlugGenerator新增的
未定义方法
:(这看起来很有希望,我明天早上第一件事就是检查一下,然后回复你。谢谢你Jocko!!不幸的是,我的应用程序找不到“slug\u generator\u类”。不确定为什么会出现这个问题,我使用的是gem的5.1.0版,实际上在我的模型中使用了“Sluged:
扩展FriendlyId friendly\u id:slug_候选者,使用:[:sluged,:finders]
更新了原始答案,在方法上有了一些变化,看看它们是否适合你。你太棒了!看起来可能有几种方法可以做到这一点,但你的方法效果很好。我还尝试添加了各种slug候选项,如果条件允许,请在为给定集合选择新slug之前先查看我的项目标题。T非常感谢您提供了明智而有用的答案!
slug\u生成器类
throws
undefined方法
并使用
crossmodelslauggenerator
throws
未定义方法Crossmodelslauggenerator
:(