I am new to rails 4 . I am using time_select to display time in my views.
While creating new event object I am getting error like:
ActiveRecord::MultiparameterAssignmentErrors (1 error(s) on assignment of
multiparameter attributes [error on assignment [12, 15] to time_begin
(Missing Parameter - time_begin(1))]):
In views:
<%= f.time_select :time_begin, :ampm => true, include_blank: true, :disabled => true %>
In my controller:
I have tried like this way:
params = event_params
hours = params['time_begin(4i)']
minutes = params['time_begin(5i)']
date = Date.strptime(params[:event_start], "%Y-%m-%d")
date = DateTime.civil(date.year,date.month, date.day, hours.to_i, minutes.to_i, 0, 0)
params[:time_begin] = date
#event = Event.new(params)
private
def event_params
params.require(:event).permit(:time_begin)
end
You need to modify your database field to be of type time not date_time. The following migration should do:
def up
change_column :events, :time_begin, :time
end
def down
change_column :events, :time_begin, :datetime
end
Note, that both types are stored same way in the database, the difference is that rails will purge (or rather reassign) its date part when you specify column to be a time. This unfortunately means that you will be able to call start_time.day, but the returned value will have no true meaning (always one), which might be a little misleading.
Related
I am trying to manually edit the "updated_at" field through a rake task
Here is what it looks like:
task :campaigns_updated_at_recovery => :environment do
Dir.foreach('db/raw-data/campaigns/') do |json|
next if json == '.' or json == '..'
file = File.read('db/raw-data/campaigns/'+json)
data_hash = JSON.parse(file)
#p data_hash["_id"]
thisCampaign = Campaign.find(data_hash["_id"])
thisCampaign["channels"].each do |chan|
if chan["updated_at"] < Date.new(2018,04,19)
data_hash["channels"].each do |channel|
if chan["_id"].to_s == channel["_id"]
chan["updated_at"] = Date.parse(channel["updated_at"])
end
end
end
thisCampaign.save
end
end
However when I run this task, the updated_at date is either not changed or updated to today's date.
What am I missing ?
I am using Mongoid and not ActiveRecord by the way
updated_at is updated by mongoid itself in a callback.
You have two solution to work around that.
The easiest solution would be to use set to change the value directly without firing any callback:
thisCampaign.set(channels: thisCampaign['channels'])
The more flexible solution would be to go down to the driver level. The basic idea is:
Campaign.collection.find(_id: data_hash["_id"]).update_one({
'$set' => {
updated_at: yourDate
}
})
Given your example, you would first need to get the full document
thisCampaign = Campaign.collection.find(_id: data_hash["_id"]).first
if thisCampaign
thisCampaign["channels"].each do |chan|
if chan["updated_at"] < Date.new(2018,04,19)
data_hash["channels"].each do |channel|
if chan["_id"].to_s == channel["_id"]
chan["updated_at"] = Date.parse(channel["updated_at"])
end
end
end
end
Campaign.collection.find(_id: data_hash["_id"]).update_one({
'$set' => {channels: thisCampaign["channels"]}
})
end
I recently refactored my DataMapper code, slowly rolling it out, and got it working on one database, but now I'm encountering problems when rolling it out to my expense database. Couldn't find the answer anywhere, and I've tried lots of fiddling.
I have a form (using Sinatra) that takes several inputs, prepended with "expense_", and it should take that data, send it to the database, and upload a receipt image to S3. But I'm getting an id of nil, and a LocalJumpError if I turn on DataMapper error reporting.
Here's my code:
DB update method:
def dm_update(method_list,model,params,param_prefix,use_last_entry_if_param_empty=true)
model_data = model.new
method_list.each do |meth|
# e.g. param is :expense_date, db column is :date
param_name = (param_prefix + meth.to_s).to_sym
param = params[param_name]
if use_last_entry_if_param_empty
# If true, use most recent entry from db - for use in settings changing
data = param.empty? ? model.last[meth] : param
else
data = param
end
model_data.send("#{meth}=", data)
end
model_data.save
end
Taking params and sending to method:
file_name = ("#{params[:expense_date]}_#{params[:expense_time].gsub(/:/, '')}_#{params[:expense_receipt_file][:filename]}")
temp_file = params[:expense_receipt_file][:tempfile]
expense_column_list = [:date,:time,:price,:currency,:description,:source,:receipt_filename]
params[:expense_receipt_filename] = file_name
dm_update(expense_column_list,Expense,params,"expense_",false)
upload(file_name, temp_file, "foo_bucket")
Datamapper class:
class Expense
include DataMapper::Resource
property :id, Serial, :required => true, :key => true
property :date, Date, :required => true
property :time, Time, :required => true, :default => Time.now
property :price, Float, :required => true
property :currency, String, :required => true, :default => "GBP"
property :description, String, :required => true
property :source, String, :required => true
property :receipt_filename, String
end
Ok, the answer to this was fairly simple when it came down to it.
I defactored the code, and went through it line by line.
The date param was stored in the format yyyy-mm-dd, and the time param was stored as hh:mm. Datamapper didn't like that time, which I'd fixed in the original version, but had failed to in the refactored version.
The fix ended up being to add the line:
temptime = Time.parse(params[:date] + " " + params[:time])
params[:time] = temptime
prior to calling the dm_update method. This fixed it, and it now works!
I am attempting to persist an object that has a Time-type field. On create, I attempt to set the sleep_value to a valid Time object but the save seems to fail silently (even though raise_on_save_failure is set to true).
If I try to search for the object via Sleep.all or Sleep.get(1), i am getting an error argument out of range.
The only value I can pass it to make it persist is Time.now(). If I do a Sleep.all, that object with sleep_time => Time.now is returned.
gems:
data_mapper (1.2.0)
sqlite3 (1.3.10)
require 'sqlite3'
require 'data_mapper'
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/test.db")
DataMapper::Model.raise_on_save_failure = true
class Sleep
include DataMapper::Resource
property :id, Serial
property :sleep_time, Time
end
DataMapper.finalize.auto_migrate!
Sleep.create(:sleep_time => Time.new(2015, 10, 1))
Sleep.all
UPDATE: I have done a bit more trial and error and have found the following values are compatible with the time field (changed the name from sleep_time to st)
Sleep.create(:st => (Time.now - 1))
Sleep.create(:st => Time.at((Time.now - Time.at(1000))))
And the following values do not save (no error is thrown but records are nt persisted)
Sleep.create(:st => Time.new(2015, 10, 1, 2, 2, 2, "+02:00"))
Sleep.create(:st => Time.new(2002))
Sleep.create(:st => Time.new((Time.now - Time.new(2002)))) #out of range
Sleep.create(:st => Time.at(2)) #out of range
I used the fact that I could use with Time.now() and made this helper method. I call it to save my Time properties.
def self.convert_to_dm_friendly_time(target_time)
today = Time.now
return today + target_time.to_i - today.to_i #basically starting the object from today and then taking today out.
end
Damn, this was an ugly solution. Surely there is an easier way?
I have a problem to approach and not sure what the most appropriate method will be to make this work. Here the background to begin:
There are two models I am working with Procedures and Appointments. The Appointments model belongs_to the Procedures model and Procedures model has_many Appointments.
Now on the procedures model there are two key points to focus on, rather, two key columns.
attr_accessible :visits, :occurence
visits is the specific number of times to schedule the Appointment(s).
occurence is the frequency of the visits. An example would be visits: "5", occurence: "weekly"
So when I am submitting my form I would like to write a method that looks at both visits: "x" and occurence: ["weekly", "biweekly", "monthly"] to then create a if or a switch -- php does switch still looking into ruby version -- but I suspect there is an elegant way to write this up.
My current create method looks like this:
def create
#appointment = Appointment.new(params[:appointment])
set_variables
if #appointment.save
flash[:success] = "Appointment scheduled!"
redirect_to patient_path(#current_patient)
else
redirect_to patient_path(#current_patient)
flash[:error] = "Appointment Date and Time cannot be blank, please try again."
end
end
What would be the best way to tackle a) identifying occurence: ["weekly", "biweekly", "monthly"] and then processing visits: "x" based on something similar to:
if #appointment.occurence == "weekly"
(x-1).times do |n|
submit same params but change within params appointment_date: = ((#appointment.appointment_date) + (n+1).week.to_formatted_s(:db)
#appointment.save
end
end
...and so on and so forth using (n+1).month for monthly occurrence (n+2).day and for bi-weekly occurrence(s).
Thank you in advance, hope this clarifies things. Just one item to note, do I need to store in database visits: and occurence:, I suspect not but would like to be certain they are used when hitting the models_controller create function.
Here's a slightly less cluttered solution that should do what you need, though it also assumes that you get rid of the :appointment_date field and change :appointment_time to a DateTime field. For more info on DateTimes check out:
(Stackoverflow will only allow me to post 2 links because I'm a n00b so search "DateTime Ruby" on your favorite search engine for documentation for the Ruby and rails methods for DateTime)
Formatting DateTime to string for views: http://apidock.com/ruby/DateTime/strftime
Intro on using DateTimes in forms: http://guides.rubyonrails.org/form_helpers.html#using-date-and-time-form-helpers
#appointment = Appointment.new(params[:appointment])
set_variables
if #appointment.save
if #procedure.occurence == "WEEKLY"
multiplier = 7
elsif #procedure.occurence == "BIWEEKLY"
multplier = 14
else
multiplier = 30
end
#visits = #procedure.visits - 1
#visits.times do |n|
Appointment.create!(
:procedure_id => #appointment.procedure_id,
:patient_id => #appointment.patient_id,
:appointment_time => (#appointment.appointment_time + (multiplier * n).days),
:attendance => "SCHEDULED"
)
end
else
flash.now[:error] = "There appears to be an error, please try again."
render 'new'
end
Solved for the moment, rather crude--as is my current ruby skill set--but it seems to have done the job.
#appointment = Appointment.new(params[:appointment])
set_variables
#appointment.save
if #procedure.occurence == "BIWEEKLY"
#visits = #procedure.visits - 1
#visits.times do |n|
procedure_id = #appointment.procedure_id
patient_id = #appointment.patient_id
appointment_date = (#appointment.appointment_date +
((n+2)*2).days).to_formatted_s(:db)
appointment_time = #appointment.appointment_time
appointment_notes = #appointment.appointment_notes
attendance = "SCHEDULED"
#scheduled = Appointment.create(procedure_id: procedure_id,
patient_id: patient_id, appointment_date: appointment_date,
appointment_time: appointment_time,
appointment_notes: appointment_notes, attendance: attendance)
end
end
if #procedure.occurence == "WEEKLY"
#visits = #procedure.visits - 1
#visits.times do |n|
procedure_id = #appointment.procedure_id
patient_id = #appointment.patient_id
appointment_date = (#appointment.appointment_date +
(n+1).week).to_formatted_s(:db)
appointment_time = #appointment.appointment_time
appointment_notes = #appointment.appointment_notes
attendance = "SCHEDULED"
#scheduled = Appointment.create(procedure_id: procedure_id,
patient_id: patient_id, appointment_date: appointment_date,
appointment_time: appointment_time,
appointment_notes: appointment_notes, attendance: attendance)
end
end
if #procedure.occurence == "MONTHLY"
#visits = #procedure.visits - 1
#visits.times do |n|
procedure_id = #appointment.procedure_id
patient_id = #appointment.patient_id
appointment_date = (#appointment.appointment_date + (n+1).month).to_formatted_s(:db)
appointment_time = #appointment.appointment_time
appointment_notes = #appointment.appointment_notes
attendance = "SCHEDULED"
#scheduled = Appointment.create(procedure_id: procedure_id,
patient_id: patient_id, appointment_date: appointment_date,
appointment_time: appointment_time,
appointment_notes: appointment_notes, attendance: attendance)
end
end
I have an HTML form which uses the following Sinatra code to handle POST for the url '/add-artist':
post '/add-artist' do
if logged_in?
a = Artist.new
a.name = params[:name]
a.website = params[:website]
a.facebook = params[:facebook]
a.created_by = session[:user_id]
a.created_at = Time.now
a.updated_by = session[:user_id]
a_updated_at = Time.now
a.views = 0
a.save
#user = User.get session[:user_id]
#user.artists.push(a.id)
#user.save
redirect '/'
end
end
The object 'a' is being saved but '#user' is not. I guess more specifically, the value '#user.artists' is not being updated. If you need more info, please ask but I have a feeling that you Ruby vets will find the problem in the code I provided.
UPDATE
Here's some additional info. I was able to reproduce the error in irb. First here's my class definition for 'User'.
# dm_models.rb
require 'data_mapper'
DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/event_review.db")
class User
include DataMapper::Resource
property :id, Serial
property :email, String
property :password, String
property :user_name, String
property :birthdate, Date
property :city, String
property :state, String
property :zip, String
property :geodata, Object
property :bio, Text
property :friends, Object
property :events, Object
property :event_reviews, Integer
property :artists, Object
property :artist_reviews, Integer
property :venues, Object
property :venue_reviews, Integer
property :created_at, DateTime
property :updated_at, DateTime
property :views, Integer
has n, :reviews
end
Here is the irb
>> require 'sinatra'
=> true
>> require 'data_mapper'
=> true
>> require './models/dm_models.rb'
=> true
>> require 'geokit'
=> true
>>
?> include Geokit::Geocoders
=> Object
>> u = User.get 8
=> #<User #id=8 #email="km#km.com" #password="km" #user_name="Katherine Miller" #birthdate=#<Date: 4895485/2,0,2299161> #city="Burbank" #state="CA" #zip="91501" #geodata=#<Geokit::GeoLoc:0x10150d4d8 #street_number=nil, #suggested_bounds=#<Geokit::Bounds:0x10150cf88 #sw=#<Geokit::LatLng:0x10150cd80 #lng=-118.315043, #lat=34.1766949>, #ne=#<Geokit::LatLng:0x10150cee8 #lng=-118.27996, #lat=34.221666>>, #lng=-118.2935891, #zip="91501", #state="CA", #precision="zip", #province=nil, #all=[#<Geokit::GeoLoc:0x10150d4d8 ...>], #street_address=nil, #provider="google", #city="Burbank", #lat=34.2039087, #country_code="US", #full_address="Burbank, CA 91501, USA", #street_name=nil, #accuracy=5, #country="USA", #success=true> #bio=<not loaded> #friends=[] #events=["13", "14", "15", "16", "28", "29"] #event_reviews=7 #artists=[] #artist_reviews=1 #venues=[] #venue_reviews=0 #created_at=#<DateTime: 70729968253/28800,-5/24,2299161> #updated_at=#<DateTime: 1178838019/480,-5/24,2299161> #views=56>
>>
?> u.artists
=> []
>> u.artists.push "5"
=> ["5"]
>> u.save
=> true
>> u = User.get 8
=> #<User #id=8 #email="km#km.com" #password="km" #user_name="Katherine Miller" #birthdate=#<Date: 4895485/2,0,2299161> #city="Burbank" #state="CA" #zip="91501" #geodata=#<Geokit::GeoLoc:0x1014e8638 #street_number=nil, #suggested_bounds=#<Geokit::Bounds:0x1014e80e8 #sw=#<Geokit::LatLng:0x1014e7eb8 #lng=-118.315043, #lat=34.1766949>, #ne=#<Geokit::LatLng:0x1014e8048 #lng=-118.27996, #lat=34.221666>>, #lng=-118.2935891, #zip="91501", #state="CA", #precision="zip", #province=nil, #all=[#<Geokit::GeoLoc:0x1014e8638 ...>], #street_address=nil, #provider="google", #city="Burbank", #lat=34.2039087, #country_code="US", #full_address="Burbank, CA 91501, USA", #street_name=nil, #accuracy=5, #country="USA", #success=true> #bio=<not loaded> #friends=[] #events=["13", "14", "15", "16", "28", "29"] #event_reviews=7 #artists=[] #artist_reviews=1 #venues=[] #venue_reviews=0 #created_at=#<DateTime: 70729968253/28800,-5/24,2299161> #updated_at=#<DateTime: 1178838019/480,-5/24,2299161> #views=56>
>> u.artists
=> []
>> u.artists.class
=> Array
The description of the above code: I retrieve user with id==8, push the value "5" into it. This appears to be successful. I save user#8. Then I re-retrieve user#8 and look at the artists value and it is an empty array.
And finally, I am able to update other fields like "artist_reviews". Is this because I am defining the datatype to be 'Object' for artists, events and venues? This problem exists for all of those fields.
Thanks for the help.
What do the logs say? Can you push to #user.artists? Is it an Array? It might fail validation and you cannot complete save.
I asked this a while ago but I'm pretty certain the solution is to serialize the object. In this case it was an array of integers. I'm not sure if I'll have the time to update this with a detailed solution but an array cannot be stored directly in a relational database. The array object must be 'serialized', essentially converted to a string. In this example the datatype for the artist attribute would then be text.
You could manually convert the artist column to an array (from string) push the new integer(s) into the array and then convert back to string and save. I'm assuming there is an automated way to do this but that's the idea.
Furthermore, this entire example is a poor way to handle associations. The better way to do this is to have a one-to-many association where there is an ArtistUser table which has two columns artist_id and user_id. Every new association is represented as a new row in the ArtistUser table.