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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:59:52+00:00 2026-05-21T23:59:52+00:00

My models seem correctly defined, because I can add as many products to an

  • 0

My models seem correctly defined, because I can add as many products to an Announcement via console.

MODELS

class Product < ActiveRecord::Base
  has_many :announcement_products
  has_many :announcements, :through => :announcement_products

  accepts_nested_attributes_for :announcements#, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end

class Announcement < ActiveRecord::Base
  has_many :announcement_products
  has_many :products, :through => :announcement_products

  accepts_nested_attributes_for :products#, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end

class AnnouncementProduct < ActiveRecord::Base
  belongs_to :announcement
  belongs_to :product
end

CONSOLE

a = Announcement.new()
a.products << Product.first
a.products << Product.last
a.name = 'foo'
a.description = 'bar'
a.save!

However, I am trying to create an announcement and add a product via a select box in a form and I get an error:

undefined method `product_id' for #<Announcement:0x00000103c1d7d0>

I tried:

THE FORM

<% form_for([:admin, @announcement], :html => { :multipart => true }) do |f| %>

  <% if @announcement.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@announcement.errors.count, "error") %> prohibited this announcement from being saved:</h2>

      <ul>
      <% @announcement.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <ul>

    <li class="clearfix">
      <%= f.label :attachment, 'Announcement image' %>

      <div class="file_field">
        <button><!-- this is skinnable --></button>
        <%= f.file_field :attachment %>
      </div>
      <p id="file_name"><%= @announcement.attachment.blank? ? '' : File.basename(@announcement.attachment.url) %></p>

      <% unless @announcement.attachment.blank? %>
        <%= image_tag @announcement.attachment.url, :class => 'attachment_image' %>
      <% end %>
    </li>

    <li class="clearfix">
      <%= f.label :name %>
      <%= f.text_field :name %>
    </li>

    <li class="clearfix mark_it_up">
      <%= f.label :description %>
      <%= f.text_area :description %>
    </li>

    <li class="clearfix">
      <%= f.label :product_id %>
      <%= f.collection_select :product_id, Product.all, :id, :name, { :prompt => 'Select a product' } %>
      <span><%= link_to 'add', new_admin_product_path %></span>
    </li>
 ....

I’m trying to add 2 products to a new Announcement record via the form. Not sure what I am doing wrong.

  • 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-05-21T23:59:53+00:00Added an answer on May 21, 2026 at 11:59 pm

    So far, I have come up with this. In my form:

    <% form_for([:admin, @announcement], :html => { :multipart => true }) do |f| %>
    
      <% if @announcement.errors.any? %>
        <div id="error_explanation">
          <h2><%= pluralize(@announcement.errors.count, "error") %> prohibited this announcement from being saved:</h2>
    
          <ul>
          <% @announcement.errors.full_messages.each do |msg| %>
            <li><%= msg %></li>
          <% end %>
          </ul>
        </div>
      <% end %>
    
      <ul>
    
        <li class="clearfix">
          <%= f.label :attachment, 'Announcement image' %>
    
          <div class="file_field">
            <button><!-- this is skinnable --></button>
            <%= f.file_field :attachment %>
          </div>
          <p id="file_name"><%= @announcement.attachment.blank? ? '' : File.basename(@announcement.attachment.url) %></p>
    
          <% unless @announcement.attachment.blank? %>
            <%= image_tag @announcement.attachment.url, :class => 'attachment_image' %>
          <% end %>
        </li>
    
        <li class="clearfix">
          <%= f.label :name %>
          <%= f.text_field :name %>
        </li>
    
        <li class="clearfix mark_it_up">
          <%= f.label :description %>
          <%= f.text_area :description %>
        </li>
    
        <li class="clearfix">
          <%= select_tag 'products[]', options_from_collection_for_select(Product.all, 'id', 'name'), :id => nil, :class => 'products' %>
          <%= select_tag 'products[]', options_from_collection_for_select(Product.all, 'id', 'name'), :id => nil, :class => 'products' %>
        </li>
     ....
    

    Then, in my controller, def create method:

      def create
        @announcement = Announcement.new(params[:announcement])
    
        @announcement.products << Product.find_all_by_id(params[:products])
    
        respond_to do |format|
          if @announcement.save
             ...
          end
        ....
    

    Is this a clean solution? Or is there a better way to do this?

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

Sidebar

Related Questions

I have a model field defined as follows: class Room(models.Model): ... ... is_course =
Models (disregard typos / minor syntax issues. It's just pseudo-code): class SecretModel(models.Model): some_unique_field =
models.py class Test(models.Model): name = models.CharField(max_length=256) slug_name = models.CharField(max_length=256) template = models.BooleanField(Is Template,default=False) @staticmethod
Are models available within the modules. Can I access constants stored in a model
I'm trying to do a ecommerce like solution. When a product is defined, there
I apologize if this is a noob question, but I can't seem to figure
I cannot seem to get my Java Swing components to work together correctly. What
I've verified my IErrorDataInfo implementation on my Model works but I can't seem to
I'm having a strange problem where I can't seem to set the initial value
My rails app has Podcast and Track models. Podcasts have many Tracks, Tracks belong

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.