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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:42:23+00:00 2026-05-27T09:42:23+00:00

This post seems good for how to create two models with one form. But

  • 0

This post seems good for how to create two models with one form. But how would you do it if the two models share one or more of the attributes?

  • 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-27T09:42:24+00:00Added an answer on May 27, 2026 at 9:42 am

    That post seems fairly outdated, I would recommend using accepts_nested_attributes_for and fields_for in your form instead. That said, overlapping attributes should probably be set in your model’s callbacks. Say you want a project’s name to be automatically set to first task’s name.

    class Project < ActiveRecord::Base
      has_many :tasks
      accepts_nested_attributes_for :tasks
      before_validation :set_name_from_task
    
      private
      def set_name_from_task
        self.name = tasks.first.name
      end
    end
    

    If your 2 models are completely unrelated, you can assign certain params to them directly in the controller.

    def create
      @foo = Foo.new(params[:foo])
      @bar = Bar.new(params[:bar])
    
      @bar.common_attr = params[:foo][:common_attr]
    
      # validation/saving logic
    end
    

    Although this is not a great practice, this logic should ideally be moved into models.

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

Sidebar

Related Questions

This seems simple but I can't figure it out. I receive post data in
Apologies if this seems like a duplicate post... Thomas Warner kindly answeres an earlier
This post reference to the One Definition Rule. Wikipedia is pretty bad on explaining
This post asks this question but doesn't really give an answer, so I thought
This post started as a question on ServerFault ( https://serverfault.com/questions/131156/user-receiving-partial-downloads ) but I determined
This post is incorrectly tagged 'send' since I cannot create new tags. I have
I know that snapshot isolation would fix this problem, but I'm wondering if NOLOCK
Maybe the solution is obvious, but I cant seem to find a good one.
This MVC tutorial suggests to make dropdownlists like so: // // POST: /StoreManager/Create [HttpPost]
Trying to get this example working from http://www.munna.shatkotha.com/blog/post/2008/10/26/Light-box-effect-with-WPF.aspx However, I can't seem to get

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.