I have an ajax post to create a new Meeting
. I want to convert the string meeting_time into a datetime format in the controller before save.
I have tried this, but it is still saving meeting_time as a string:
def create
requestor = current_user
@meeting_with_params = meeting_params
@meeting_time = @meeting_with_params[:meeting_time]
@meeting_time = @meeting_time.to_datetime
@meeting_with_params[:meeting_time] = @meeting_time
#@meeting_with_params
@meeting = Meeting.new(@meeting_with_params)
respond_to do |format|
if @meeting.save
format.html { redirect_to home_url, notice: 'Your lex was successfully requested! Click Plan Meeting ' }
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
The params being passed look like this:
Started POST "/meetings" for 127.0.0.1 at 2016-10-31 11:45:44 -0400
Processing by MeetingsController#create as */*
Parameters: {"meeting"=>{"requestor_id"=>"1", "requestee_id"=>"2", "meeting_time"=>"2016-10-18 22:10:00", "accepted"=>"false", "status"=>"Active", "location"=>"Black Forest Restaurant", "location_address"=>"733 Fulton St, Brooklyn, NY 11217"}}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
(0.1ms) begin transaction
SQL (1.1ms) INSERT INTO "meetings" ("requestor_id", "requestee_id", "meeting_time", "accepted", "location", "status", "location_address", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) [["requestor_id", 1], ["requestee_id", 2], ["meeting_time", "2016-10-18 22:10:00.000000"], ["accepted", "f"], ["location", "Black Forest Restaurant"], ["status", "Active"], ["location_address", "733 Fulton St, Brooklyn, NY 11217"], ["created_at", "2016-10-31 15:45:44.557266"], ["updated_at", "2016-10-31 15:45:44.557266"]]
And the ajax call in in .js file:
$.ajax({
url: '/meetings',
type: 'POST',
data: {
meeting: {
requestor_id: currentUserId,
requestee_id: requesteeId,
meeting_time: rubyDateTime,
accepted: false,
status: "Active",
location: meetingLocation,
location_address: meetingAddress
}
},
success: function(result) {
createNotification(currentUserId, "Your meeting was successfully requested.");
location.reload();
},
error: function(err) {
console.log(err);
}
})
How can I pull out meeting_time in the create controller, and change it to datetime before saving?
Thanks!!
****UPDATE I know it is not saving correctly because I also have a method that compares today's date with the meeting_time, and if it has passed, to do something. When saving directly to the database, it recognizes the times that have passed. When saving through the POST, with the exact same string, it does not since it is comparing 2 different types. The comparision controller:
def requestor_review_meeting
@past_meetings = Meeting.where('meeting_time <= ? AND requestor_id= ? AND status = ? AND requestor_score < ?', DateTime.current, current_user, "Active", 1).order('meeting_time asc');
render json: @past_meetings
end
And in console:
> meeting_time
=> Tue, 18 Oct 2016 22:10:00 UTC +00:00
2.1.2 :012 > meeting_time.class
=> ActiveSupport::TimeWithZone
2.1.2 :013 > meeting_time.is_a?(DateTime)
=> false
2.1.2 :015 > meeting_time.is_a?(Time)
=> true
There is no need to convert the datetime string into datetime before save, because it will be saved as datetime automatically.
Ex)
Consider first field is user name and second one is DOB. The second one will be automatically parsed as datetime.
first string from above '1990-10-31' treated as date second string '11:45:44' treated as time and third string '0000' denotes timezone
It seems, your code looks fine. If you try to print
meeting_time
by querying the post, it should be datetime object.Update
As said by @Rocco, the following is enough to save the data as Rails will convert the date string to date automatically from params.
ActiveRecord automatically type casts all inputs so they match the database schema. So even though the allow you to save
meeting_time
as a string, it will convert it to a datetime on load. If you're interested you can read more here