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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:45:38+00:00 2026-05-26T04:45:38+00:00

When I go to my donors_controller#create from a form in my view, it will

  • 0

When I go to my donors_controller#create from a form in my view, it will update a record if it matches on ID or the collection of columns in my find_by_*

But if I call that create from a different controller, it always creates new records.

My donors_controller has a create method with:

  def create
    # need to find donor by id if given, else use find_or_create_by_blahblahblah
    unless @donor = Donor.find_by_id(params[:donor][:id])
      @donor = Donor.find_or_initialize_by_company_and_prefix1_and_first_name1_and_last_name1_and_address1(params[:donor])
    end
    if @donor.new_record?  
       ...
    else
       ...
    end
  end

My other controller has :

 class_name = 'Donor'
 klass = ActiveRecord.const_get(class_name)  
   ... code to populate myarray with appropriate values
 klass.create(myarray)

I am pretty sure myarray is populated with the necessary params since it creates valid records with everything in the right place. I can even run the same record through more than once and it creates duplicate (except for the Donor.id of course) records.

What am I doing wrong here?

I noticed I could do this in my other controller and it works, but why can’t I call the create from the donors_controller and have it work without always creating a new record?

#klass.create(myarray)
@mydonor = klass.find_or_initialize_by_company_and_prefix1_and_first_name1_and_last_name1_and_address1(myarray) 
@mydonor.save
  • 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-26T04:45:38+00:00Added an answer on May 26, 2026 at 4:45 am

    your question is very unclear and hard to follow, so i’m not sure my answer will match your needs. It seems you’re confusing a controller#create method with a model#create method.

    On the model : it is a class method

    create, as its name implies, instantiates a new object of the Donor class and calls save on it:

    @donor = Donor.create
    # is the same thing as
    @donor = Donor.new
    @donor.save
    

    Active Record uses new_record? to determine if it should perform an insert or an update on the db when you call save on an instanciated record. So with create it will always be an insert since the record is inevitably new ; but if you call save on a record retrieved from the database, it will be updated.

    On a controller : it is an instance method

    … but does not directly manages persistence, that’s the role of the model. It is an action for this controller ; it is called create for the sake of RESTful naming conventions. create is called on a controller instance when a POST request is routed to it ; its purpose is to manage that request, which often (but not always) means to create records using the appropriate model, using YourModelName.new or YourModelName.create.

    so it is absolutely possible (but not advisable) to do:

    class DonorsController < ApplicationController
      def create
        # only works well if params[:donor][:id] is nil,
        # will raise an error if a record with the  
        # same id exists in the database
        @donor = Donor.create(params[:donor]) 
      end
    end
    

    as it is generally needed to perform several operations before saving your record, and to check if save is successful, the process is broken down :

    class DonorsController < ApplicationController
      def create
        @donor = Donor.new(params[:donor])
        # perform operations on @donor (instance of Donor class)
        if @donor.save # save returns true if successfully performed, false either
          # all is fine
        else
          # @donor could not be saved
        end
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a donation form on my website that forwards donors to
I'm trying to get a single record from my news feed. I pass it
I'm trying to create an animation that would look like 2 french doors (or
I've got a mysql query that is returning the required data, but is not
I've quickly made up a sliding doors example but I'm incorporating it into my
I am using a rails helper that creates 3 select lists for the purpose
Here is my query (SELECT * FROM `jokes` WHERE `flags` < 5 AND (`title`
We have a client that is closing its doors. We want to redirect all
Tags array(11) { [album] => string(17) Away From The Sun [artist] => string(12) 3
I'm trying to style a button with the css 'sliding doors' technique, but it

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.