HI
i would like to write a method that returns all the events that are on tomorrow or within the next 24 hours.
e.g
def tomorrows_events
#events = Event.will_occur_in next_24_hours
end
i have a datetime for each event which is called so to get it it would be, #event.date_and_time
i have the search logic gem installed but don't know if it supports dates, i couldn't find anything on it.
what would be the best way to write it? is there something in search logic i can use?
thanks
Event.all(:conditions => { :date_and_time => (Time.now.tomorrow.beginning_of_day)..Time.now.tomorrow.end_of_day})
I wrote a plugin that'll help you do this: http://github.com/radar/by_star.
You'd use this command for it:
Event.tomorrow(Time.zone.now, :field => "date_and_time")
Then it will return only results for tomorrow.
Related
I suspect my question is stupid and answer is obvious, but I TOTALLY stuck with it.
In controller movies_controller.rb I have method index, which fills the array #movies with values from database by calling #movies = Movie.all, and method sort, which do almost the same, but use #movies = Movie.order(:title) instead.
If I call sort explicitly from index what I see in View Index.html.haml is sorted by title list of movies as I expect. But, how can I call method sort by clicking on link? I try
In Index.html.haml:
%th= link_to 'Movies title', 'movies', :on_click => 'self.sort'
In routes.rb:
match ":controller => movies, :action => sort" => 'movies/index'
and nothing is happening, method sort never executes. Actually, I have no idea how to do it right. Looks like it is obvious for everyone, but me.
Can I find short answer how to do it right? Please, kick me in right direction!
If you are looking for sorting data in table then you need some plug in to in place sort the table according to some attribute. following jquery plugin help you to do that,
https://github.com/linjunpop/jquery-tablesorter-rails
or you can achieve the same with background sorting with order clause. check out the following,
http://railscasts.com/episodes/228-sortable-table-columns
For quick and easy you can use 'ransack' gem.
For some writing by yourself check this out http://railscasts.com/episodes/228-sortable-table-columns
I do:
#deals = Deal.find(:all)
I use #deals for a number of things. Every deal has a value field (how much money the deal is worth). I want to know the combined value of all deals. I have this now:
#deals.each { |deal| #total_value += deal.value }
But I'm hoping and guessing ActiveRecords have a better way to do this? Is there?
Try following:-
#deals_value = Deal.sum(:value)
Thanks....
Assuming that you want to keep Deal.find(:all), and you want to use #deals to find the sum without a loop, try the following
#deals.sum(&:value)
I am currently looking on converting Thu Jun 10 16:17:55 +0530 2010 to 15/06/2010 or 15-06-2010 using Ruby
I am getting the dates from the DB as shown below.
#tickets = Ticket.find(:all)
for ticket in #tickets
print ticket.created_at
end
I have been looking around for resources as I a n00b in RoR/Ruby.
I have looked at
Class Date
Class DateTime
and couldn't make much of it.
Please let me know how I could achieve this conversion in Ruby.
P.S: I have tried using regular expressions as well. But with no luck
If you are using Rails (which is indicated by your tag)
Date.today.to_s can take a parameter which represents a key in your DATE_FORMATS hash
http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Date/Conversions.html#M000915
http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/Time/Conversions.html#M000837
A much better solution than #strftime, since its reusable without having to remember the #strftime incantation or having loads of helpers to do the same thing.
Do this.
#tickets = Ticket.find(:all)
for ticket in #tickets
print ticket.created_at.strftime("%m/%d/%Y")
end
For complete reference of strftime see here
#tickets = Ticket.find(:all)
for ticket in #tickets
print ticket.created_at.strftime("%d/%m/%Y")
end
Also you may want to check out the strftime method in Ruby's stdlib.
This question already has answers here:
Rails ActiveRecord date between
(11 answers)
Closed 9 years ago.
I'm trying to figure the best way to query a date range from rails...I looked around on Google but am unsure about how to use this syntax.
I have a Model that has various events and I like to add to my find condition a caveat that should only show events where the field :st_date is today or later, in effect only show me data that is current, nothing that happened before today.
I ran into a problem because I have no end date to stop the query, I want to query everything from today to next month.
I was thinking something like
#events = Event.find(:all, :conditions => ["start_date between ? and ?",
date.Today, date.next_month.beginning_of_month])
but I get the error undefined local variable or method `date'......
Do I need do anything particular to use the Date class? Or is there something wrong with my query syntax? I would really appreciate any help.
You want Date.today, not date.today. There's nothing wrong with what you're doing, you're just not referencing the date class properly
Furthermore it would be Date.today.next_month.beginning_of_month
I would take it a step further and define a scope in your model for reuse.
# rails 3 example:
# app/models/event.rb
scope :upcoming, lambda {
where("start_date between ? and ?", Date.today, Date.today.next_month.beginning_of_month)
}
# app/controllers/some_controller.rb
#events = Event.upcoming
There is also a great Railscasts episode on scopes in Rails 3:
http://railscasts.com/episodes/202-active-record-queries-in-rails-3
Event.where(:start_date => Date.today..Date.today.next_month.beginning_of_month) also works great.
Take a look at my by_star plugin which lets you do things like:
Event.by_month(Time.now, :field => "start_date")
I'd like to show in our app when the latest production deploy was made, as a means of showing an ongoing commitment to improvement, etc, etc.
Off the top of my head I'm thinking of getting a last_updated_at from one of the files, but I'd like to hear other thoughts.
What's the best way to get the date of the latest production deploy dynamically?
Thanks,
Josh
Thanks to mpd who pointed me on the right direction.
For those interested in doing something similar, this is a quick and dirty method that probably can be refined and refactored.
In app/controllers/application_controller.rb put this method in the private section:
private
def app_last_updated_at
if File.exist?(RAILS_ROOT + "/REVISION")
timezone = "Mountain Time (US & Canada)"
#app_last_updated_at = File.atime(RAILS_ROOT + "/REVISION").in_time_zone( timezone )
else
#app_last_updated_at = "Not Long Ago."
end
end
Obviously, replace the timezone with your own (or you can do something fancy for individual user timezones).
In order to have this run all the time I use a :before_filter and put it at the top of your application_controller.rb.
before_filter :app_last_updated_at
And then to actually show this last updated at date, you just throw this or something like it in a layout or a partial or whatever:
<%=
unless #app_last_updated_at.nil?
if #app_last_updated_at.is_a? Time
#app_last_updated_at.to_s(:long)
else
#app_last_updated_at
end
end
%>
Hopefully, this helps others. I'm not keen on having it run in the ApplicationController for every access, so suggestions would be appreciated.
You can do it pretty easily with Capistrano. Take a look at this link I think it does exactly what you want Deployment date