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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:40:56+00:00 2026-06-09T13:40:56+00:00

I have two model classes: Goal and Objective. A Goal has many Objectives, and

  • 0

I have two model classes: Goal and Objective. A Goal has many Objectives, and an Objective belongs to one Goal.

I have created a page for creating a new Objective via a form. In the page’s controller, I am setting the value of a variable @default_goal like so:

@objective = Objective.new

if params.has_key?(:default_goal)
    @default_goal = Goal.find(params[:default_goal])
end

Then, in my form I want to make that variable the default Goal in an association dropdown list. I tried the following but it doesn’t seem to work:

<%= simple_form_for @objective do |f| %>
    <%= f.association :goal, :prompt => "Select a goal", :default => @default_goal %>
<% end %>

How can it be done?

  • 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-09T13:40:57+00:00Added an answer on June 9, 2026 at 1:40 pm

    Ah, the answer was really obvious. Instead of setting the default in the view form, it should be done on the record itself in the controller. Example:

    @objective = Objective.new
    
    if params.has_key?(:default_goal)
        @objective.goal = Goal.find(params[:default_goal])
    end
    

    That will automatically select it as the default option in the view form’s association list.

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

Sidebar

Related Questions

I have a two model classes which have relationship of one to many. public
I have a simple one-to-many (models.ForeignKey) relationship between two of my model classes: class
I have two model classes, say parent and child. Parents are created first, and
I have two classes (MVC view model) which inherits from one abstract base class.
I have two classes in a model . One is Sdr_Layer class . Other
I'm creating a little calendar app in Django. I have two model classes; Calendar
I have two model classes, each is a table in a database. One model
I have two questions. 1: At the moment I have two model classes that
Given two model classes, Foo and Bar , I want Foo to have 3
I have a hibernate model with two classes, let's say A and B. B

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.