Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6925649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:49:01+00:00 2026-05-27T10:49:01+00:00

Have STI classes: class Page < ActiveRecord::Base belongs_to :user end class FirstTypePage < Page

  • 0

Have STI classes:

class Page < ActiveRecord::Base
  belongs_to :user
end

class FirstTypePage < Page
end

class SecondTypePage < Page
end

Controllers for each class,

class PageController < AplicationCorroller
end

class FirstTypePageController < PageController
end

class SecondTypePageController < PageController
end

And routings:

resources :user
  resource :page
end

How to handle FirstTypePage by FirstTypePageController, SecondTypePage by SecondTypePageController on single path?

i.e.

user/1/page/2 is handled by:
FirstTypePageController if “page 2” type is “FirstTypePage”,
and by SecondTypePageController if “page 2” type is “SecondTypePage” ?

UPDATE: My solution:

  match 'user/:user_id/page/:action',
    :controller=>'page/first_type_page',
    :constraints=>PageConstraints.new('FirstTypePage')
  match 'user/:user_id/page/:action',
    :controller=>'page/second_type_page',
    :constraints=>PageConstraints.new('SecondTypePage')

class PageConstraints

  @@cache ||= {}

  def initialize o_type
    #@mutex = Mutex.new
    @o_type = o_type
  end

  def matches?(request)
    user_id = request.params[:user_id]
    #add Mutex lock here
    unless page_type = @@cache[user_id]
      page_type = User.find(user_id).do_some_magik_to_suggest_type
      @@cache[page_id] = page_type
      @@cache.shift if @@cache.size > 1000
    end
    page_type == @o_type
  end

end

I think this solution will work fast on a small amount of page types, and we can manage memory size, used for routings on a large amount of pages

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:49:02+00:00Added an answer on May 27, 2026 at 10:49 am

    I can see one option to do that – preload all pages in the routes.rb and define special routes for each page.

    resources :users do |user|
      Page.all do |page|
        if page.first_type?
          # ... routes to first_type_page_controller
        else
          # ...
      end
    end
    

    Another solution could be to use strategy patter in the PageController (no need to use FirstTypePageController and other).

    pages_controller.rb:

    before_filter :choose_strategy
    
    def show
      @strategy.show
    end
    
    private
    
    def choose_strategy
      @strategy = PagesControllerStrategy.new(self, page)
    end
    
    def page
      @page ||= Page.find params[:id]
    end
    

    pages_controller_strategy.rb:

    class PagesControllerStrategy
    
      def initialize(controller, page)
        @controller = controller
        @page = page
      end
    
      def show
        # do what you what with controller and page
      end
    end
    

    However, I’d suggest you to split the behavior on the view level only:

    show.html.haml:

    - if page.first_type?
      = render 'pages/first_type'
    - else
      // ...
    

    EDIT:

    I just found another solution, that could help you – custom constraints.
    http://railsdispatch.com/posts/rails-3-makes-life-better

    I’m not sure if that works in your case, but I think it is worth to play with routes more.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have STI implementation as follows: class Automobile < ActiveRecord::Base end class Car <
I have the following: class Series < ActiveRecord::Base has_many :components end class Component <
I am using STI for my user models. I have an User class, and
Here's what I'm thinking: class Widget < ActiveRecord::Base has_one :widget_layout end class WidgetLayout <
Let's say I have a class like: class Basket < ActiveRecord::Base has_many :fruits Where
I have a model object which subclasses ActiveRecord. Additionally, using STI, I have defined
I have an abstract Base class. Let's call it Animal::Base . module Animal class
I have a Rails app that uses STI to handle different types of Users,
I have three models: User Award Trophy The associations are: User has many awards
I have four models: User Award Badge GameWeek The associations are as follows: User

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.