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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:08:35+00:00 2026-06-04T13:08:35+00:00

I’m using rails 3.2.3 and am having trouble assigning a value in my HABTM

  • 0

I’m using rails 3.2.3 and am having trouble assigning a value in my HABTM relation.

Basically I have got 2 models, Product and User.
When I create a new Product, I want to insert in the Products_Users middle table the inserted values.

It has to be a HABTM as in the future I want to be able to say “user x can also edit that product”, instead of just the product’s original creator.

My models: (other relations omitted for simplicity)

product.rb
class Product < ActiveRecord::Base
has_and_belongs_to_many :users
accepts_nested_attributes_for :users

attr_accessible <product attributes>, :user_ids, :user_id #it says cant mass assign user_ids so I also added the :user_id, but then it says unknown attribute :user_id
end

user.rb

class User < ActiveRecord::Base
has_and_belongs_to_many :products
attr_accessible :user_id <plus the user attributes>
end

My products controller has the default actions and code from scaffolding.

The problem may also lie in my View. I also have a HABTM relation between products and categories and it as all fine (I choose the categories from a checkbox list)

But how can I insert in the Products_Users table the new line after an already logged user creates a new product?

Im trying to use in my view after in the form:

<%= f.hidden_field :product_id, :value => params[:id]%> #What to put here if the product isn’t tecnically created?

<%= f.hidden_field :user_id, :value => current_user.id%>#This “current_user.id” works in other views.

This is not working, and makes little sense also, is it possible to achieve what I am trying to do?

Thanks for your help

  • 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-04T13:08:36+00:00Added an answer on June 4, 2026 at 1:08 pm

    I deleted the hidden fields and added this after the save:

    user=current_user
    @product.users << user
    

    Hope it helps someone.

    Regards

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am reading a book about Javascript and jQuery and using one of 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.