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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:56:03+00:00 2026-06-10T20:56:03+00:00

I have two models Client and Topic . Both of which have a HABTM

  • 0

I have two models Client and Topic. Both of which have a HABTM association between them.

I am trying to add a select statement in my _form partial in my Client views, that allows the user to add a topic to a client (or edit that topic, etc.).

This is what my form partial looks like:

<%= form_for(@client) do |f| %>

  <div class="field">
    <%= f.label :topic %><br />
    <%= f.select :topics, Topic.all.collect { |topic| [topic.name, topic.id] }, {:include_blank => 'None'} %>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

The first error I got was this:

ActiveModel::MassAssignmentSecurity::Error in ClientsController#create

Can't mass-assign protected attributes: topics

So, in my Client model, I added this:

attr_accessible :email, :firm_id, :name, :phone, :topics

This is the error I now get:

NoMethodError in ClientsController#create

undefined method `each' for "1":String

The create action of my Clients controller is very standard:

  def create
    @client = Client.new(params[:client])

    respond_to do |format|
      if @client.save
        format.html { redirect_to @client, notice: 'Client was successfully created.' }
        format.json { render json: @client, status: :created, location: @client }
      else
        format.html { render action: "new" }
        format.json { render json: @client.errors, status: :unprocessable_entity }
      end
    end
  end

These are the params submitted (notice that topics is being passed – instead of topic_id but topic_id doesn’t work either):

{"utf8"=>"✓",
 "authenticity_token"=>"J172LuZQc5NYoiMSzDD3oY9vGmxxCX0OdxcGm4GSPv8=",
 "client"=>{"name"=>"Jack Daniels",
 "email"=>"jack.daniels@some-email.com",
 "phone"=>"2345540098",
 "firm_id"=>"2",
 "topics"=>"1"},
 "commit"=>"Create Client"}

How can I get a topic assigned to my client on the creation of the client with this select statement?

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-06-10T20:56:04+00:00Added an answer on June 10, 2026 at 8:56 pm

    When setting a “Topic” attribute, Client expects an instance of a Topic class.

    Since you are passing IDs, you need to change:

    <%= f.select :topics, Topic.all.collect { |topic| [topic.name, topic.id] }, {:include_blank => 'None'} %>
    

    to set topic_ids instead:

    <%= f.select :topic_ids, Topic.all.collect { |topic| [topic.name, topic.id] }, {:include_blank => 'None'} %>
    

    And, of course, in attr_accessible:

    attr_accessible :email, :firm_id, :name, :phone, :topic_ids
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my Rails app, I have two models, Estimate and Client , which both
I have two models User and Category that have a HABTM association. I would
So I have two models Topic , Client . A Client has_and_belongs_to_many :topics ,
I have two models, Reports(belongs to client) and Clients(has many reports). A Client has
I have two models Clients and Reports. A Client has_many Reports and a Report
I have two models. I am trying to query a model for how many
I have two models, Client and PaymentOptions . class Client(models.Model): name = models.CharField(max_length=50, null=True,
I have two models, Client and PaymentOptions . class Client(models.Model): name = models.CharField(max_length=50, null=True,
I have a Django project with two models: Applicant and Client, where Client is
I have an app which makes use of two models: Movie and Tag. The

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.