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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:48:42+00:00 2026-06-09T22:48:42+00:00

Consider a Store that has_many Products which have_many Opinions. Here are Models: Store: class

  • 0

Consider a Store that has_many Products which have_many Opinions. Here are Models:

Store:

class Store < ActiveRecord::Base
    attr_accessible :desc, :location, :name, :phone, :status, :url

    has_many :products
    has_many :opinions, :through => :products
end

Product:

class Product < ActiveRecord::Base
    attr_accessible :desc, :name, :status, :url

    belongs_to :store
    has_many :opinions
end

finally, Opinion:

class Opinion < ActiveRecord::Base
    attr_accessible :content, :eval, :status

    belongs_to :store
    belongs_to :product
end

To create a new opinion (that belongs to a product and a store), here is the create method of OpinionsController:

def create

    # Get product info.
    product = Product.find_by_id params[:opinion][:product_id]

    # Create a new opinion to this product
    opinion = product.opinions.build params[:opinion]
    opinion.status = 'ON'

    if opinion.save
        redirect_to :back, :notice => 'success'
    else
        redirect_to :back, :alert => 'failure'
    end
end

But here is the resulted error: Can't mass-assign protected attributes: product_id

Question: How can I pass the product_id to the controller?

Tell me if you need more info.

Thanks in advance.

  • 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-09T22:48:44+00:00Added an answer on June 9, 2026 at 10:48 pm

    This looks like a scenario where you could use nested resource routing http://guides.rubyonrails.org/routing.html#nested-resources

    However, if you just want a quick fix in your Opinion controller you just need to omit the product_id when building the Opinion. Something like this should work:

    # Get product info.
    product = Product.find_by_id params[:opinion].delete(:product_id)  # delete removes and returns the product id
    
    # Create a new opinion to this product
    opinion = product.opinions.build params[:opinion]  # Since there is no product id anymore, this should not cause a mass assignment error.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following setup: class Parent < ActiveRecord::Base has_many :children end class Child <
Consider my Event class, and that i store DateTime 's in the DB as
Consider a M:M relation that needs to be represented in a Cassandra data store.
I have a class whose instances have attributes that are containers which themselves contain
In an answer to Is it safe to store objects of a class which
Consider a grocery store scenario (I'm making this up) where you have FACT records
Consider this code: import socket store = [] scount = 0 while True: scount+=1
Would you consider it bad practice to store various user data in a database
This is something that has been pulling at me for a while. Consider a
I'm working on a C# application that needs to store all the successive revisions

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.