How should I design nested url?

For now, I'm setting up my nested routing like this But in this case,

  • example.com/shop/walmart/topic/1 <= if I create a topic first time
  • example.com/shop/bestbuy/topic/2 <= if I create a topic second time
  • example.com/shop/walmart/topic/3 <= if I create a topic third time

In spite of the fact there are only 2 records that belong to walmart shop The ID will be shown as '3' How can I make this ID as count up style? Should I prepare another column or something?

resources :communities, :path => "shop", do
resources :community_topics, :path => "topic", :as => :'topic'
end

Answers


Does it really matter if the number in the url is the id of the record? If that matters to you, you can make a number 'slug' similar to your shop slug "walmart", "bestbuy" etc. You'll have to make a new column in the topics table and use a before_create filter to increment that value. Something like this:

class Topic
  before_validation :increment_slug, :on :create
  validates_uniqueness_of :slug, scope: :shop_id

  private
  def increment_slug
    self.slug = Topic.where("shop_id = ?", shop_id).order("slug DESC").limit(1).slug + 1
  end
end

Make sure 'slug' here is a numeric field so the + and sorting work correctly.


Need Your Help

Visual Studio. Code changes don't do anything

c# visual-studio-2010 compilation

Any changes I make to my code aren't doing anything. I've even tried putting lines in that I know will crash my program, and nothing. It just keep running the old version. It's even loading old ver...

Mongoid datatype retrieval

ruby-on-rails mongoid

Is it possible to quickly retrieve the datatype of a given Mongoid field?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.