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.