2

I have a Survey class and typical CRUD methods, including one create method. I want to create a second create method named create_pre, which does more nuanced survey creation.

Here is my code:

  def create #old create survey method
    @survey = Survey.new(survey_params)

    respond_to do |format|
      if @survey.save
        format.html { redirect_to(@survey, :notice => 'Survey was successfully created.') }
        format.xml  { render :xml => @survey, :status => :created, :location => @survey }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @survey.errors, :status => :unprocessable_entity }
      end
    end
  end

and the new one:

  def create_pre #new create survey method
    @survey = Survey.new(survey_params)

    respond_to do |format|
      if @survey.save
        format.html { redirect_to(@survey, :notice => 'Survey was successfully created.') }
        format.xml  { render :xml => @survey, :status => :created, :location => @survey }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @survey.errors, :status => :unprocessable_entity }
      end
    end
  end

but when I am trying to use the new one from the console, I am getting an error:

 s = Survey.create_pre name: 'Levi\'s Pre survey', intervention_id: 165242, template_id: 3
NoMethodError: undefined method `create_pre' for Survey (call 'Survey.connection' to establish a connection):Class
Did you mean?  create
    from /home/levi/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activerecord-4.2.4/lib/active_record/dynamic_matchers.rb:26:in `method_missing'

Don't know how to fix this?

When I am calling create, a survey gets created and saved in the db.

irb(main):022:0* s = Survey.create name: 'Levi\'s Pre survey', intervention_id: 165242, template_id: 3
  SQL (5.1ms)  USE [evrrprod2012]
  SQL (4.1ms)  BEGIN TRANSACTION
  SQL (6.0ms)  EXEC sp_executesql N'INSERT INTO [surveys] ([name], [intervention_id], [template_id], [created_at], [updated_at]) OUTPUT INSERTED.[id] VALUES (@0, @1, @2, @3, @4)', N'@0 nvarchar(4000), @1 int, @2 int, @3 datetime, @4 datetime', @0 = N'Levi''s Pre survey', @1 = 165242, @2 = 3, @3 = '02-25-2016 13:09:15.37', @4 = '02-25-2016 13:09:15.37'  [["name", "Levi's Pre survey"], ["intervention_id", 165242], ["template_id", 3], ["created_at", Thu, 25 Feb 2016 18:09:15 UTC +00:00], ["updated_at", Thu, 25 Feb 2016 18:09:15 UTC +00:00]]
  SQL (2.9ms)  COMMIT TRANSACTION
=> #<Survey id: 40, template_id: 3, name: "Levi's Pre survey", created_at: "2016-02-25 18:09:15", updated_at: "2016-02-25 18:09:15", intervention_id: 165242, created_by: nil, skip_reason: nil, skipped: nil, label: nil, note: nil, parent_id: nil>

Same attempt when using create_pre fails.

1 Answer 1

2

Survey.create_pre is trying to call a class method on the Survey class, which by convention will be a model, not your controller.

Update

Just addressing the new comment and addition from OP to their question.

The Survey.create method you've added is nothing to do with the SurveysController#create method you're showing earlier in your question.

The one you're showing is a controller instance method that you've defined yourself, in your controller, to handle an incoming request. The one you're calling in your console, Survey.create is an ActiveRecord::Base class method that's available in your Survey model because it inherits from ActiveRecord::Base.

The create method in SurveysController has nothing to do with the Survey.create method you're able to call in your console, and likewise the new create_pre method you've added to SurveysController cannot be called on the Survey model.

Sign up to request clarification or add additional context in comments.

5 Comments

That call is for survey's intervention and is defined in interventions.rb (a model). However even if I am removing create_pre entirely and I am duplicating exactly create as create_pre, I am still getting the same error: NoMethodError: undefined method `create_pre'.
The create_pre that you show above is obviously a controller method, it will be defined in something like app/controllers/surverys_controller.rb - I don't know why you think it has anything to do with either the survey or intervention models.
The create method is in the same controller. Both methods, create and create pre are in the same surveys_controller.rb . I wll edit the question to eliminate any confusions with other things like interventions.
Your question didn't contain anything confusing about interventions, it was your comment to my answer where you started speaking about interventions. So anyway, as you say now, both create and create_pre methods are instance methods on SurveysController and yet in your console you're calling Survey.create_pre - ie. as I said in my answer, you're trying to call a class method on Survey!
I will edit again to add the successful result when calling Survey.create !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.