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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T11:30:06+00:00 2026-06-16T11:30:06+00:00

Suppose I have Post HABTM Tag and I am using mass asignment through nested

  • 0

Suppose I have Post HABTM Tag and I am using mass asignment through nested attributes.

I have this in Post model:

accepts_nested_attributes_for :posts_tags, \
                              :reject_if => proc { |attrs| attrs.tag_id.blank? }

I have this in Post controller:

def new
  @post = Post.new
  3.times { @post.posts_tags.build }
end

def create
  @post = Post.new(params[:post])
  @post.save
end

And this in Post form:

<%= f.fields_for :tags do |tg| %>
  <%= tg.label :tag_id %>
  <%= tg.select :tag_id .... %>
<% end %>

Everything works just perfectly and with minimal code. The Post gets accociated with Tags that were selected.

And now: What if I want the users to select at least one Tag for their Post. How can I invalidate Post that has no Tag selected? What would be the most elegant solution?

  • 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-16T11:30:07+00:00Added an answer on June 16, 2026 at 11:30 am

    Add validates_presence_of :tags in Post model to force the user to select the tag.

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

Sidebar

Related Questions

-- This is an updated post, old post removed -- Suppose I have data
Suppose I have a blog post entity. It has many attributes It has comments
Suppose I have a Post class and a Tag class. The relationship between Post
Suppose I have a Django app named blog. There's a model called Post and
Suppose I have a Post model which has_many Comments. I want to get the
Suppose I have an action: function actionShowItem($id) { $item = Item::model()->findByPk($id); $this->render(showitem,array('model' => $id));
Suppose I have this model: class PhotoAlbum(models.Model): title = models.CharField(max_length=128) author = models.CharField(max_length=128) class
Suppose I have an Comment model that belongs_to a Post model. I want to
Suppose I have a Post model and I'd like to run a method when
Regarding this post and this other one . Suppose I have the following: public

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.