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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:43:38+00:00 2026-05-23T15:43:38+00:00

Here are my models: class Lesson < ActiveRecord::Base belongs_to :topic, :polymorphic => true validates_presence_of

  • 0

Here are my models:

class Lesson < ActiveRecord::Base
  belongs_to :topic, :polymorphic => true  
  validates_presence_of :topic_type, :topic_id  
end

class Subject < ActiveRecord::Base  
   has_many :lessons, :as => :topic  
end

class Category < ActiveRecord::Base  
  has_many :lessons, :as => :topic  
end

Now, what I need is a form that will allow the user to create or update Lessons. The questions is, how can I provide a select menu that offers a mix of Subjects and Categories? (To the user, on this particular form, Subjects and Categories are interchangeable, but that’s not the case elsewhere.)

Ideally, this would look something like this:

views/lessons/_form.html.haml

= simple_form_for(@lesson) do |f|
  = f.input :title
  = f.association :topic, :collection => (@subjects + @categories)

That won’t work because we’d only be specifying the topic_id, and we need the topic_types as well. But how can we specify those values?

I guess the crux of the problem is that I really want a single select menu that specifies two values corresponding to two different attributes (topic_id and topic_type). Is there any elegant railsy way to do this?

A few notes:

a) Single table inheritance would make this issue go away, but I’d like to avoid this, as Categories and Subjects have their own relationship… I’ll spare you the details.

b) I might could pull some javascript shenanigans, yes? But that sounds messy, and if there’s a cleaner way to do it, some magic form helper or something, then that’s certainly preferable.

c) Though I’m using simple_form, I’m not wedded to it, in case that’s complicating matters.

Thanks

  • 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-23T15:43:39+00:00Added an answer on May 23, 2026 at 3:43 pm

    If you don’t wish to use STI, you can do something similar: create a new model Topic(name:string) which will polymorphically reference Subject or Category.

    class Lesson < ActiveRecord::Base
      belongs_to :topic
      validates_presence_of :topic_id  
    end
    
    class Topic < ActiveRecord::Base
      belongs_to :topicable, :polymorphic => true
    end
    
    class Subject < ActiveRecord::Base
       has_one :topic, :as => :topicable
       has_many :lessons, :through => :topic
       accepts_nested_attributes_for :topic
    end
    
    class Category < ActiveRecord::Base  
       has_one :topic, :as => :topicable
       has_many :lessons, :through => :topic
       accepts_nested_attributes_for :topic
    end
    

    In the view where you create a new Subject/Category:

    <%= form_for @subject do |subject_form| %>
      <%= subject_form.fields_for :topic do |topic_fields| %>
        <%= topic_fields.text_field :name %>
      <% end %>
    <% end %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here are my models: class BillingProfile < ActiveRecord::Base belongs_to :student attr_accessible :cost end class
Here are my models: class Deck < ActiveRecord::Base belongs_to :game has_many :deck_cards end class
So I have two models here: class Screen < ActiveRecord::Base belongs_to :user validates :screen_size,
Here is my models: class User < ActiveRecord::Base has_one :worker, :class_name => 'Worker', :foreign_key
Here's part of my Django app's models.py : class Person(models.Model): birth_year = WideYear(null=True, blank=True)
Here's my code: class Publisher(models.Model): name = models.CharField( max_length = 200, unique = True,
Here is a related manager I wrote: class PortfolioItemManager(models.Manager): use_for_related_fields = True def extended(self):
Here is a Django model I'm using. class Person(models.Model): surname = models.CharField(max_length=255, null=True, blank=True)
I have a nested form with the following models: class Incident < ActiveRecord::Base has_many
Here are my models: class Groups(models.Model): group_id = models.AutoField(primary_key=True) group_name = models.CharField(max_length=20,db_index=True) #Some other

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.