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

The Archive Base Latest Questions

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

having those example models: class Post < ActiveRecord::Base belongs_to :category end class Category <

  • 0

having those example models:

class Post < ActiveRecord::Base
  belongs_to :category
end

class Category < ActiveRecord::Base
  has_many :posts
end

Imagine I have a form to create new posts, by default I get a select with a list of available categories, but what If I want to create new categories from the “new/edit post” form?

Would be perfect to have a button with the select box that popup a windows to create a new category.

Is better than creating a new category and then going to create a new post.

What I always saw is creating has_many associations, but not belongs_to one.

Thank you

  • 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-15T08:41:04+00:00Added an answer on June 15, 2026 at 8:41 am

    I think you’re kind of asking a couple questions here. I’m going to say the more important one is how to create the association in the opposite direction from how it’s usually built (because that’s the one I know how to answer). I’ll focus on that.

    For simplicity, I’ll just define a simple text field with label you can fill in as part of your form.

    = label_tag :new_category_name
    = text_field_tag :new_category_name
    

    In your controller, you can then build the new category like so.

    @post.build_category(name: params[:new_category_name])
    

    If you’re saving your @post the conventional way, then the category will be created in the same transaction as the post, so if that fails it won’t create the category. If you want it to save the category no matter what you can call @post.create_category instead.

    Here’s the documentation:
    http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

    In the table Singular associations (one-to-one), replace other with your model name.

    The other question I think is how to make it look good in the view. You certainly have options for how to make it look good. You can create a popup as you suggested. You could also use AJAX to send a small xhr request. I’ve seen fancy combination select / text boxes too.

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

Sidebar

Related Questions

I have a model which uses acts-as-tree. For example: class CartoonCharacter < ActiveRecord::Base acts_as_tree
I have 3 django models (simplified for this example): class Fighter (models.Model): name =
I have a model with two foreign keys: class Model1(models.Model): model_a = models.ForeignKey(ModelA) model_b
Having read many of Matthew Weier O'Phinney's posts regarding implementing ACL into Models, I
I want to have dynamic parameters, so I made these models : class DynamicObject(models.Model):
I'm having trouble looping through a set of dynamically created UITextFields and storing those
I am having trouble with some of my rubygems, in particular those that use
Having these generic interface and class: interface TestIntf<T extends TestIntf<T>> { void test(T param);
I have been having these really odd problems with Visual Studio 2010. At this
I am having trouble simply making multiple pages with CodeIgniter. For example, I am

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.