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 3941712
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:34:32+00:00 2026-05-20T00:34:32+00:00

I’m trying to create a factory_build class method for creating Redirect objects within an

  • 0

I’m trying to create a factory_build class method for creating Redirect objects within an ActiveRecord collection (see below).

For example @website.redirects.factory_build(:code => 301) would return a new instance of a PermenantRedirect object. However, even though I am able to invoke the factory_build class method I can’t get access to the primary_key for @website. Is there a way to access @website.id from inside a collection or am i forced to pass it into the method call? Any other suggestions would also be welcome, maybe I’m not going about this in the right approach. thanks.

class Website < ActiveRecord::Base
  has_many :redirects
  has_many :permenant_redirects
  has_many :temporary_redirects
end

# Redirect is an abstract class it uses Rails Single Table Inheritance
class Redirect < ActiveRecord::Base
  def self.factory_build(attributes)
    status_code = attributes.delete(:code)
    case status_code
      when 301
        Website.find( ... ).permenant_redirects.new(attributes)
      when 302
        Website.find( ... ).temporary_redirects.new(attributes)
      else
        raise InvalidStatusCodeError
    end
  end
end

class TemporaryRedirect < Redirect
  def status
    302
  end
end

class PermenantRedirect < Redirect
  def status
    301
  end
end
  • 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-20T00:34:33+00:00Added an answer on May 20, 2026 at 12:34 am

    You need to access the scope for this relationship in order to be able to properly construct these objects. This is available to a class method:

    class Redirect < ActiveRecord::Base
      def self.factory_build(attributes)
        redirect_type =
          case (status_code = attributes.delete(:code))
          when 301
            PermanentRedirect
          when 302
            TemporaryRedirect
          else
            Redirect
          end
    
        redirect_type.new(attributes.merge(scoped.scope_for_create))
      end
    end
    

    I’ve taken the liberty of DRYing up your creation code because you had a lot of duplication in there. Also note that calling find only to exercise a relationship is a waste of resources. If you know the ID of the record, simply pass that along as an attribute unless you have a very good reason for retrieving it first.

    The scoped.scope_for_create call returns attributes defined by your current scope or scopes. These are usually in the form of something like { 'website_id' => 101 } and can be passed through to your create call.

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

Sidebar

Related Questions

No related questions found

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.