I am saving data to two different models at once. This has successfully been done.
These two models are associated with each other, so one most store the others ID on save. How to I store the questionnaire_contact_id in QuestionnaireResult?
class QuestionnaireResultsController < ApplicationController
def create
#questionnaire_result = QuestionnaireResult.new(params[:questionnaire_result])
#questionnaire_contact = QuestionnaireContact.new(params[:questionnaire_contact])
respond_to do |format|
if #questionnaire_result.save
#questionnaire_contact.save
format.html { redirect_to root_path, notice: 'Questionnaire was successfully submited.' }
format.json { render json: questionnaires_path, status: :created, location: questionnaires_path }
else
format.html { render action: "new" }
format.json { render json: questionnaires_path.errors, status: :unprocessable_entity }
end
end
end
end
You should use activerecord associations:
def create
#questionnaire_result = QuestionnaireResult.new(params[:questionnaire_result])
#questionnaire_contact = #questionnaire_result.questionnaire_contacts.new(params[:questionnaire_contact])
respond_to do |format|
if #questionnaire_result.save #this line will automatically save associated contact
# code
else
# code
end
end
end
Solved, it was as easy as doing this:
class QuestionnaireResultsController < ApplicationController
def create
#questionnaire_result = QuestionnaireResult.new(params[:questionnaire_result])
#questionnaire_contact = QuestionnaireContact.new(params[:questionnaire_contact])
respond_to do |format|
#questionnaire_contact.save
#questionnaire_result.admin_questionnaire_contact_id = #questionnaire_contact.id
if #questionnaire_result.save
format.html { redirect_to root_path, notice: 'Questionnaire was successfully submited.' }
format.json { render json: questionnaires_path, status: :created, location: questionnaires_path }
else
format.html { render action: "new" }
format.json { render json: questionnaires_path.errors, status: :unprocessable_entity }
end
end
end
end
Related
I've searched through the rails docs but not find any method which can check if record was corectly updated from outside transaction block because I dont want to put respond_to block into my transaction because it is bad practise. Is there any method like persisted? which I use with create/save method...
def update
ActiveRecord::Base.transaction do
address = Address.find_or_create_by(address_params)
#invoice_account = InvoiceAccount.find_or_create_by(invoice_account_params
.merge({ :invoice_address_id => address.id }))
#invoice = Invoice.find_by_id(params[:id])
#invoice.update(invoice_params.merge({ purchaser_id: #invoice_account.id }))
end
respond_to do |format|
if #invoice.method `some method to check if invoice was updated`
format.html { redirect_to invoice_url(#invoice), notice: "Invoice was successfully updated." }
format.json { render :show, status: :ok, location: #invoice }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: #invoice.errors, status: :unprocessable_entity }
end
end
end
I just edit my code as bellow, capture result of the transaction to variable is_updated. When in transaction error occurred then is_updated would be false and changes are rolled back. If no error occurred is_updated true.
def update
is_updated =
ActiveRecord::Base.transaction do
address = Address.find_or_create_by(address_params)
#invoice_account = InvoiceAccount.find_or_create_by(invoice_account_params
.merge({ :invoice_address_id => address.id }))
#invoice = Invoice.find_by_id(params[:id])
#invoice.update(invoice_params.merge({ purchaser_id: #invoice_account.id }))
end
respond_to do |format|
if is_updated
format.html { redirect_to invoice_url(#invoice), notice: "Invoice was successfully updated." }
format.json { render :show, status: :ok, location: #invoice }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: #invoice.errors, status: :unprocessable_entity }
end
end end
Calling .update on an ActiveRecord object will return true if it was able to store it to db or false if any error occurred.
I'd take advantage of that and store its result on a variable, like the following:
def update
updated = false
ActiveRecord::Base.transaction do
address = Address.find_or_create_by(address_params)
#invoice_account = InvoiceAccount.find_or_create_by(invoice_account_params
.merge({ :invoice_address_id => address.id }))
#invoice = Invoice.find_by_id(params[:id])
updated = #invoice.update(invoice_params.merge({ purchaser_id: #invoice_account.id }))
end
respond_to do |format|
if updated
format.html { redirect_to invoice_url(#invoice), notice: "Invoice was successfully updated." }
format.json { render :show, status: :ok, location: #invoice }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: #invoice.errors, status: :unprocessable_entity }
end
end
end
I'm building an app like reddit where you add a Submission to a specific user page. Submission has a few attribute including an attribute called url.
I want to check when adding a new submission if already a submission with that same url exists for that specific page, and if it exist, vote for it instead of creating it as a new submission. If not, just add it as a new submission. I'm using the act_as_votable gem here.
Here is the create method:
def create
#user = User.friendly.find(params[:user_id])
#submission = #user.submissions.new(submission_params)
#submission.member_id = current_user.id
#submission.creator_id = #user.id
#submission.is_viewed = false
#submission.get_thumb_and_title_by_url(#submission.url)
respond_to do |format|
if #submission.save
format.html { redirect_to #user, notice: 'Submission was
successfully created.' }
format.json { render :show, status: :created, location: #submission }
else
format.html { render :new }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
you should take a look at https://apidock.com/rails/v4.0.2/ActiveRecord/Relation/find_or_create_by and https://apidock.com/rails/v4.0.2/ActiveRecord/Relation/find_or_initialize_by
Now on your code we can make changes like
def create
#user = User.friendly.find(params[:user_id])
#submission = #user.submissions.find_or_initialize_by(submission_params)
if #submission.id.present?
# What to do if the record exists
else
# if its a new record
#submission.member_id = current_user.id
#submission.creator_id = #user.id
#submission.is_viewed = false
#submission.get_thumb_and_title_by_url(#submission.url)
end
respond_to do |format|
if #submission.save
format.html { redirect_to #user, notice: 'Submission was
successfully created.' }
format.json { render :show, status: :created, location: #submission }
else
format.html { render :new }
format.json { render json: #user.errors, status: :unprocessable_entity }
end
end
end
I hope that this can put you on the right track
Happy Coding
In my def Create funciton in casting_controller. I create an Casting object and save it. this is ok, but i also want to create a LinkCastingToModel object, insert data to it from my controller, but when i check, the data is always nil. How can i insert data into it
def create
#casting = Casting.new(casting_params)
#casting.models_booked = 0
link = LinkModelAndCasting.new
link.client_id = #casting.id
link.save
# link_model_and_casting = LinkModelAndCasting.new(:casting_id => #casting.id)
# link_model_and_casting.save
respond_to do |format|
if #casting.save
format.html { redirect_to #casting, notice: 'Casting was successfully created.' }
format.json { render :show, status: :created, location: #casting }
else
format.html { render :new }
format.json { render json: #casting.errors, status: :unprocessable_entity }
end
end
end
I use postgresql, thanks.
It's because when you're assigning clinet_id to link from #casting.id, till then #casting was not saved, so the id is actually nil.
You'll have to call #casting.save before that. Then it will work. Something like this:
def create
#casting = Casting.new(casting_params)
#casting.models_booked = 0
#casting.save
link = LinkModelAndCasting.new
link.client_id = #casting.id
link.save
# link_model_and_casting = LinkModelAndCasting.new(:casting_id => #casting.id)
# link_model_and_casting.save
respond_to do |format|
if #casting.id
format.html { redirect_to #casting, notice: 'Casting was successfully created.' }
format.json { render :show, status: :created, location: #casting }
else
format.html { render :new }
format.json { render json: #casting.errors, status: :unprocessable_entity }
end
end
end
In my Calendar app both registered and unregistered users can create a meeting. In my meetings_controller :
def new
#meeting = Meeting.new
#meeting = current_user.meetings.new(meeting_params) if current_user
end
def create
respond_to do |format|
if #meeting.save
format.html { redirect_to #meeting, notice: 'Meeting was successfully created.' }
format.json { render :show, status: :created, location: #meeting }
else
format.html { render :new }
format.json { render json: #meeting.errors, status: :unprocessable_entity }
end
end
end
def meeting_params
params.require(:meeting).permit(:name, :start_time)
end
So, if a current_user exists it creates current_user.meetings.new(meeting_params) and if not, it should create just a Meeting.new without any user. However, it doesn't work and I get an error:
undefined method `save' for nil:NilClass
respond_to do |format|
if #meeting.save
format.html { redirect_to #meeting, notice: 'Meeting was successfully created.' }
format.json { render :show, status: :created, location: #meeting }
else
It works well if there is a current_user, but why the meeting without a user defines as 'nill' if I mentioned that it shout be just a Meeting_new? How can I make it work?
Thank you!
Change your controller code as like :
def new
#meeting = Meeting.new
end
def create
#meeting = Meeting.new(meeting_params) #edit
#meeting.user_id = current_user.id if current_user
respond_to do |format|
if #meeting.save
format.html { redirect_to #meeting, notice: 'Meeting was successfully created.' }
format.json { render :show, status: :created, location: #meeting }
else
format.html { render :new }
format.json { render json: #meeting.errors, status: :unprocessable_entity }
end
end
end
def meeting_params
params.require(:meeting).permit(:name, :start_time, :user_id)
end
I need to save items separately coming from a form of a text field, but my code is saving these items duplicate form.
My controller
def create
#answer_option = AnswerOption.break_options(answer_option_params)
#answer_option = AnswerOption.new(answer_option_params)
respond_to do |format|
if #answer_option.save
format.html { redirect_to #answer_option, notice: 'Answer option was successfully created.' }
format.json { render :show, status: :created, location: #answer_option }
else
format.html { render :new }
format.json { render json: #answer_option.errors, status: :unprocessable_entity }
end
end
end
My model
class AnswerOption < ActiveRecord::Base
belongs_to :question
def self.break_options(var)
ugly_answers = var[:content].split /[\r\n]+/
ugly_answers.each do |answer|
AnswerOption.create!(content: answer)
end
end
end
Thanks!
def create
#answer_option = AnswerOption.break_options(answer_option_params)
end