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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:40:43+00:00 2026-05-20T17:40:43+00:00

I cannot figure out how I can setup a form that will create a

  • 0

I cannot figure out how I can setup a form that will create a new Study while also creating the related StudySubject and the Facility. The user_id, facility_id and study_subject_id have to be available to create the Study object as you can see in the database relation model.

Database model

Here is the migration for the studies. The other tables do not contain foreign keys.

def self.up
 create_table :studies do |t|
  t.references :user
  t.references :facility
  t.references :subject
  t.date "from"
  t.date "till"
  t.timestamps
 end
 add_index :studies, ["user_id", "facility_id", "subject_id"], :unique => true
end

The models define the following associations.

# user.rb
has_many :studies

# subject.rb
has_many :studies

# facility.rb
has_many :studies

# study
belongs_to :user
belongs_to :subject
belongs_to :facility

Questions

1) Are the has_many and belongs_to definitions correct?
2) How can I create a study using accepts_nested_attributes_for?
3) A study should only belong to one user. Do I need to add the user_id into every other object to store the association?

I am totally new to Rails since 2 weeks of extensive learning. Sorry for a stupid question maybe.

  • 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-20T17:40:44+00:00Added an answer on May 20, 2026 at 5:40 pm

    Yeah. It works. A good friend offered his help. This is what we set up.
    Please mind that I renamed StudySubject to Subject in the meantime.

    The model study.rb

    belongs_to :student, :class_name => "User", :foreign_key => "user_id"  
    belongs_to :subject  
    belongs_to :university, :class_name => "Facility", :foreign_key => "facility_id"  
    
    accepts_nested_attributes_for :subject, :university
    

    The controller studies_controller.rb

    def new
      @study = Study.new
      @study.subject = Subject.new
      @study.university = Facility.new
    end
    
    def create
      @study = Study.new(params[:study])
      @study.student = current_user
    
      if @study.save
        flash[:notice] = "Successfully created study."
        redirect_to(:action => 'index')
      else
        render('new')
      end
    end
    

    I use devise for authentication and cancan for authorization. That is why current_user is available in the controller.

    The new study view new.html.erb

    <%= form_for @study, :url => { :action => "create" } do |f| %>
    
      <table summary="Study form fields">
    
        <%= render :partial => "shared/study_form_fields", :locals =>  { :f => f } %>
    
        <%= f.fields_for :subject do |builder| %>
          <%= render :partial => "shared/subject_form_fields", :locals =>  { :f => builder } %>
        <% end %>
    
        <%= f.fields_for :university do |builder| %>
          <%= render :partial => "shared/facility_form_fields", :locals =>  { :f => builder } %>
        <% end %>
    
      </table>
    
      <p><%= f.submit "Submit" %></p>
    
    <% end %>
    

    I hope this will save you some time. I spent a lot of time to realize how things have to be set up.

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

Sidebar

Related Questions

I'm working on an ipad webapp that will receive monthly changes. However I can
I cannot figure out json for whatever reason, i don't understand why i cannot
For some reason, I cannot figure out how to return the value of an
Related: Lazy datatypes in Objective C From the related question I was able to
This seems simple enough. Elmah logs the errors, I see the error in the
Is there a way to reuse parts across multiple buldouts? I've got several tools
I've been to the edge of the internet and on support lines trying to
I use checkstyle to check if my java code respects the guidelines of our
Some 3rd party keyboards have more than one character on each key, for example
I am trying to customize date from my RSS feed to display like how

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.