time_zone select in Rails without region - ruby-on-rails-3

I am trying to use the time_zone_select helper with Rails which outputs the select
<%= f.time_zone_select(:time_zone) %>
HTML output is:
<option value="Belgrade">(GMT+01:00) Belgrade</option>
All my timezones I have been calculated using a separate rake task that has stored the time zone as a string in my database in the format of:
Europe/Belgrade
This causes issues with editing the record as the time_zone_select doesn't store the values with the region. What am I doing wrong or what can I do to get around this?
**More info****
This rake task I have created creates and stores time zones using ActiveSupport::TimeZone.
When I create a time zone object, the name attribute returns a string "Europe/Belgrade" but the time zone helper expects this value to be "Belgrade"

Related

" rake aborted! unknown attribute: date " while running rake db:seed

rake aborted!
unknown attribute: date
I am finally able to migrate my database content but encounter this unfortunate error.
Is it because the date attributes are older than the actual new database or something? Date is not specifically defined in my model but obviously the data was in the old database before and def not manually entered. I think t.timestamp takes care of that initialization, so i guess the question is why I cannot seed into my database? Any ideas
My seed file looks something like this:
Indication.create([
{ :name => "general", :date => "2012-11-09 17:36:25" },
Looks like your Indication model does not have a date field. Do you need to create a migration to add one?
So just in case someone encounters the same problem, here is my solution:
My seed:dump had extracted the data in this case "2012-11-09 17:36:25" and assigned it the variable "date". While I am still not sure why this happened it has to do with the t.timestamp command.
t.timestamp creates 2 variables:
created_at
updated_at
The side file had the 2012-11-09 17:36:25 as "date" variable, which is a reasonable guess based on the format, but nevertheless it is wrong for the Rails application standard. My tables had only two variables that were datetime format which are the ones I named above.
I opened the seeds file and replaced all the ":date" for ":created_at" and it solved the problem. updated_at then acquired the value automatically (updated at was not transferred from my old database to this new one).
The created_as data did transfer and was correct. updated_at acquired the same value in all the data (I noticed it was the date and time and performed the rake db:seed:load command.

How do I change the format ActiveRecord expects when parsing dates from a text field in a form?

The problem
I have a Ruby on Rails model with a Date attribute.
In the form for this model, I am using a single text field with a JQuery datepicker to represent this attribute (not a drop down for each of year, month, and day, as is the Rails custom).
The datepicker inserts dates with a mm/dd/yyyy format.
Rails is expecting dates with a dd/mm/yyyy format.
Examples
If a user selects March 12th, 2012, the datepicker puts 03/12/2012, which is interpreted by Rails as December 3rd, 2012.
If a user selects March 20th, 2012, the datepicker puts 03/20/2012, which is interpreted by Rails as the 3rd day of the 20th month of 2012. Since this date doesn't exist, Rails casts this to a nil value (I think).
Question
How do I change the date format Rails uses when parsing this date text field?
Notes:
1) I do not want to change the format of the date the datepicker inserts into the text field,
2) I am not asking about displaying my date attribute in a view.
I initially thought this could be solved through the Rails internationalization features, but it turns out I was wrong.
Ever since Ruby 1.9, the standard format for date parsing is dd/mm/yyyy, so as to better accomodate international users. More details can be found in this SO answer.
That standard is maintained in Rails, as Date.parse is now used to process data from form inputs. Using a before_validation callback won't work because the field is going to be received as nil by the callback method.
Right now there are two gems dealing with this specific issue, namely that date parsing in Rails does not follow the locale settings from I18n.locale. Both seem to work well.
delocalize, by clemens - Seems to have been applied successfully in a decent number or projects and has the highest number of stars at the moment.
i18n_alchemy by carlosantoniodasilva - This one has been released more recently. The author is a Rails core team member, and a very active one at that. Definitely deserves a look.
Since you don't want to change the picker's format, I would suggest you use a hidden field for the actual model property.
For example, add a hidden field for the model's date property, assuming you use a form builder as usual:
f.hidden_field :date
Then for the picker text input, don't bind it to the model's date property. Let's say the hidden field has ID 'modelname_date' and the picker text input has ID 'date_picker', use the following to make it work:
$(function(){
$("#date_picker").datepicker({altField: '#nodelname_date', altFormat: 'dd/mm/yyyy'});
});
In this way the date picker shows the date as 'mm/dd/yyyy' but Rails will see the date as 'dd/mm/yyyy'.
Update:
If you want to work this out on the Rails side, here's another solution I'd suggest:
Add a virtual property to your model: attr_accessor :bad_format_date
Add a before_validation callback in which you parse the input date and assign it to the real field:
before_validation do
self.date = Date.strptime(bad_format_date, "%m/%d/%Y")
end
Then for the form on the view use bad_format_date but initialize it with the date field value (if it's an edit form).
The timeliness gem makes ruby date/time parsing much more customizeable and integrates well with Rails.
Since you're working with Rails, be sure to check out the validates_timeliness project as well by the same guy. It includes all of timeliness plus sophisticated date/time validation methods for ActiveModel.
You could try do something like this.
$(function(){
$('#date_picker').datepicker( {
beforeShowDay: $.datepicker.noWeekends,
showOtherMonths: true,
selectOtherMonths: true,
dateFormat: 'dd-mm-yy',
defaultDate: date,
gotoCurrent: true
});
I just add the following monkey patch to config/time_formats.rb
class Date
class << self
alias :euro_parse :_parse
def _parse(str,comp=false)
str = str.to_s.strip
if str == ''
{}
elsif str =~ /^(\d{1,2})[-\/](\d{1,2})[-\/](\d{2,4})/
year,month,day = $3.to_i,$1,$2
date,*rest = str.split(' ')
year += (year < 35 ? 2000 : 1900) if year < 100
euro_parse("#{year}-#{month}-#{day} #{rest.join(' ')}",comp)
else
euro_parse(str,comp)
end
end
end
end

Rails 3.2.8 - How to handle Time zones?

I've participated the Rails Rumble this year and I found myself tangled by time zone issues.
I uploaded my app in linode and I live in EST time zone or -4 to UTC.
I have a model and it saves things by doing the following:
def self.processing_creation(user_id, home_id, chore_id)
registered_chore = RegisteredChore.where("DATE(created_at) = ?", Date.today).find_by_user_id_and_home_id_and_chore_id(user_id, home_id, chore_id)
unless registered_chore
registered_chore = RegisteredChore.create(user_id: user_id, home_id: home_id, chore_id: chore_id, created_at: Time.zone.now)
user = registered_chore.user
user.add_one_chore_point
end
registered_chore
end
RegisteredChore.where("DATE(created_at) = ?", Date.today).find_by_user_id_and_home_id_and_chore_id(user_id, home_id, chore_id)
returns false even if the data was just created.
I noticed that "create" saves in UTC but Date.today uses user's local time zone.
What's the best way to handle this?
Another example to illustrate the issue:
I want to register a chore at 11 PM in EST time.
The server is already in the next day (e.g: 12).
Rails saves the data in the next day date (12).
But user is still in day 11.
Technically, user with the current method, could save the entry again because the days are different from the db and user interface.
How to solve this?
Rails will, by default, save times to the database as UTC. If you're using DATE([Date.today]), then it's going to be looking up records on the 11th, not the 12th. To get the right date, you probably want to set Time.zone to the user's current timezone, and then do the query.
I built a gem called by_star to handle this kind of date searching, and I reckon you should use it. With it, your query would be this:
RegisteredChore.today.where(:user_id => user.id,
:home_id => home.id,
:chore_id => chore.id)
Rails 3 by default saves all the time in database relative in GMT+00:00 time zone. So you will have to set an offset while computing time depending on your timezone. Else, you can change default Activerecord time zone by adding following to application.rb
config.time_zone = 'Your time zone' (Example: 'Eastern Time (US & Canada)')
config.active_record.default_timezone = 'Your time zone' (Example: 'Eastern Time (US & Canada)')

Rails 3 & i18n: "Object must be a Date, DateTime or Time object" error

I am trying to translate my Rails 3 application, read the primer at http://guides.rubyonrails.org/i18n.html#adding-date-time-formats and subsequently downloaded the corresponding yml file from https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale (de.yml in my case).
In one of my views I had this code (some_model#index) ...
<td><%= time_ago_in_words(some_model.created_at) %></td>
... which I changed in ...
<td><%=l time_ago_in_words(some_model.created_at) %></td>.
Unfortunately this gives me this error:
Object must be a Date, DateTime or Time object. "etwa ein Monat" given.
Any idea why this fails? The created_at column has been created in the database via standard Rails scaffolding (database is mysql using mysql2 gem).
If I strip the time_ago_in_words helper from the code ...
<td><%=l some_model.created_at %></td>.
... the translation works - but the datetime now is of course too long for my <td>.
I also tried to duplicated the distance_in_words section of the de.yml and rename it to time_ago_in_words but this did not work either.
Am I missing something obvious?
OK, there are 2 different methods in play here :
the l method takes a Date, a Time or a DateTime object and returns a formatted version based on your I18n rules.
the time_ago_words takes the same arguments and uses I18n to spit out a formatted string.
In your example, you're trying to use both! Put simply, all you need is <%= time_ago_in_words(some_model.created_at) %>.

Store time interval in PostgreSQL from Rails

I need to store time inverval in PosgreSQL. Data will be put from Ruby on Rails.
Say, PizzaHut accepts orders from 9:00 to 18:00.
I've created following migration:
class AddOrderTimeAndDeliveryTimeToMerchants < ActiveRecord::Migration
def self.up
add_column :merchants, :order_time, :interval
end
I've read the documentation, and have following code now:
Merchant.create( :delivery_time => "9:00 18:00" )
When i execute it, i get following error message:
PGError: ERROR: invalid input syntax for type interval: "9:00 18:00"
How to do it correctly ?
I don't think an interval is really what you want for that. An interval represents a timespan without any specific end points; for example, you add an interval to an existing time to get another time. You would be better off with two distinct times:
add_column :merchants, :order_from, :time, :null => false
add_column :merchants, :order_to, :time, :null => false
Then, if for some reason you need to know how many hours they're open for delivery, you can construct an interval by subtracting :order_from from :order_to.
If you really must use an interval, then you'll need to construct a value something like this:
:delivery_time => "interval '11 hour'"
Note how this illustrates that an interval is not a specific time range from A to B, it is just a time range of some certain length (with no specified end points).
It's likely that you want a time without timezone here, since if Dominoes in NY opens at 9:00 local time, and Dominoes in California also opens at 9:00 local time, then a time with a timezone would not work properly.
What you likely want is one of two things. either two times a start and an end time, or a start time and an interval. I would suggest two times, but the choice is yours. Note that you can an interval from two times by subtracting one from the other.
select '09:00:00'::time - '05:00:00'::time;
?column?
----------
04:00:00