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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:13:53+00:00 2026-06-10T23:13:53+00:00

I’ve got an Event and Activity. Each event has one activity, and the user

  • 0

I’ve got an Event and Activity. Each event has one activity, and the user needs to pick from a large number of activities. I’ve got a collection_select field that shows properly in the Event form, but it won’t save. I keep getting a Couldn't find Activity without an ID error.

I thought that by creating the has_one belongs_to association in my models that the forms would automatically know what to do. Nope. So I started adding methods to the Events controller, not even knowing if they are correct. But things didn’t break.

Now I’m thinking that I probably need to create a local variable in the form, but I’ve tried a dozen variations and can’t get anything to work. I just end up getting other error messages.

My Event model

class Event < ActiveRecord::Base

  attr_accessible :date, :activity

  has_one :activity
  belongs_to :creator, :class_name => "User"
  has_many :attendees, :class_name => "User"
  has_many :comments
end

Activity model

class Activity < ActiveRecord::Base
  attr_accessible :location, :title, :image, :image_cache

  mount_uploader :image, ImageUploader

  belongs_to :event
end

part of Events controller

def new
  @event = Event.new
  @activities = Activity.find(:all, :order => 'title')

end

def create
  @event = Event.new(params[:event])
  @activities = Activity.find(:all, :order => 'title')
  @activity = Activity.find(params[:id])
end

Events form

<div class="field">
  <h6>When do you want to go?</h6><br />
  <%= f.text_field :date %>
</div>
<h5>Activities</h5>

<%= collection_select(:activity, :event_id, Activity.all, :id, :title, {:include_blank => 'Please Select'} ) %>


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

Wherein lies my ignorance?

  • 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-10T23:13:55+00:00Added an answer on June 10, 2026 at 11:13 pm

    When doing this you need to use a nested form structure in order to update both models at the same time. You need to add accepts_nested_attributes_for to your Event model.rb file. Below are what each of your files should look like.

    Event Model

    class Event < ActiveRecord::Base
      attr_accessible :date, :activity
    
      has_one :activity
      accepts_nested_attributes_for :activity
    
      belongs_to :creator, :class_name => "User"
      has_many :attendees, :class_name => "User"
      has_many :comments
    end
    

    Activity Model is good bc you already have the belongs_to event

    Event Controller:

    def new
      @event = Event.new
      activity = @event.activity.build
    end
    
    def create
      @event = Event.new(params[:event])
      if @event.save
        #etc.
      end
    end
    

    Form

    <%= form_for @event do |f| %>  
         <div class="field">
          <h6>When do you want to go?</h6><br />
          <%= f.text_field :date %>
        </div>
    
        <h5>Activities</h5>
        <%= f.fields_for :activity do |b| %>
           <!--Add whatever form input you need for your activity.--> 
           <%= b.collection_select :title %>
        <% end %>
    
        <div class="actions">
          <%= f.submit %>
        </div>
    <%end%>
    

    If you want a good walk through to look at checkout the railscast on nested model forms

    In response to the questions in the comments…yes I would reverse you model relationship setup so you have…

    class Activity < ActiveRecord::Base
      has_many :events
    end
    
    class Event < ActiveRecord::Base
      belongs_to :activity
    end
    

    this way each event will have an activity_id column which will identify which activity corresponds with it and is used for rails to make the association.

    Now for the many_to_many associations, which it sounds like you will need, you have two options. You can use has_many through or HABTM. The simplest rule of thumb is that you should set up a has_many :through relationship if you think you will need to work with the relationship model as an independent entity. Check out this rails guide for more explanation and details about all the methods these associations will create.

    I always like has_many through since you always have the option to work with the relationship if you choose to in the future so here is how you would do that

    class Event < ActiveRecord::Base
      has_many :plans
      has_many :users, :through => :plans
    end
    
    class Plan < ActiveRecord::Base
      belongs_to :event
      belongs_to :user
    end 
    
    class User < ActiveRecord::Base
      has_many :plans
      has_many :events, :through => :plans
    end 
    

    You can call the relationship whatever you want, “plan” is just what I thought of first.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
In my XML file chapters tag has more chapter tag.i need to display chapters

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.