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

  • SEARCH
  • Home
  • 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 8725535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:03:42+00:00 2026-06-13T08:03:42+00:00

I have templates and template versions. A template can have many template_versions, but only

  • 0

I have templates and template versions. A template can have many template_versions, but only one active template_version at any given time. I have the following two models:

class Template < ActiveRecord:Base
    has_many :template_versions, :class_name => 'TemplateVersion'
    belongs_to :active_version, :class_name => 'TemplateVersion'
end

class TemplateVersion < ActiveRecord:Base
    belongs_to :template
    has_one :template
end

It’s critical that a template only have one active template_version, which is why the key to the active_template is on the template model. This all seems fine until I test it in a rails console:

t = Template.new()
tv = TemplateVersion.new()
t.active_version = tv
t.save

version = t.active_version //returns version
version.template_id //returns nil

The template knows about its active template_version, but the problem is that the template_version doesn’t know what template it belongs to. I’m guessing this is because on the insert to the DB, the template_version is created to get the id to associate the template with, which would then have to be saved to hand back the template id to populate the template version.

Is there a way of accomplishing all this?

  • 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-06-13T08:03:42+00:00Added an answer on June 13, 2026 at 8:03 am

    The problem with your current setup is you’ve defined two “template” methods for TemplateVersion. If I have a tv object, tv.template could be the has_one or belongs_to template, ActiveRecord doesn’t know which. I’m not sure if you can alias those somehow.

    A workaround: add an “active” column to your TemplateVersion model and validate there’s only one active TemplateVersion

    class Template < ActiveRecord::Base
        has_many :template_versions, :class_name => 'TemplateVersion'
        has_one :active_version, :class_name => 'TemplateVersion', :conditions => ['active = ?', true]
    end
    
    class TemplateVersion < ActiveRecord::Base
        attr_accessible :template_id, :active
        belongs_to :template
        validates :only_one_active
    
        def only_one_active
          errors.add(:base, "Only one active version per template") if self.active == true and TemplateVersion.where(:active => true, :template_id => self.template_id).count > 0
        end
    
    end
    

    You can then access the active version as t.active_version, but to set the active version you’d need to make that update on the the TemplateVersion.

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

Sidebar

Related Questions

I have a problem with nested templates and their template specialization. Given the following
I have seen many versions of this question but the answers always turn into
I have a index.php file that will include several external files: content/templates/id1/template.php content/templates/id2/template.php content/templates/id3/template.php
Hallo I have this script: <? require(lib2/config.inc.php); require(lib2/tpl.class.php); require(lib2/db.class.php); require(lib2/um.class.php); $tpl = new template(templates,
i have template base.html and some children templates. I don't know how to set
I have found several similar answers but I can't quite get why this is
Is it possible to have multiple versions of the same class which differ only
I have a dozen of xslt templates and most of them using one specific
I am new to Dynamic Component Templates. I have created a Component Template and
I have a table contents many fields db.define_table('i2l_templates', Field('id','id', represent=lambda id:SPAN(A(T('View'),_href=URL('view_template',args=id)),' | ', A(T('Edit'),_href=URL('edit_template',args=id)))),

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.