Calendar_date_select gem for Rails 3.2 - ruby-on-rails-3

I want both date and time to select in rails 3 application the "Calendar_date_select" is not working in rails 3.
can any one tell any jquery or gem which work on rails 3 to select both date and time
Thanks in advance

I prefer http://jqueryui.com/datepicker/ to select date in different formats

Related

When an user views all articles, see their own articles first

we have two models
article
user
user have many articles
i need When an user views all articles, see their own articles first.
my idea
make two query
first query return articles related to query
Article.where(user_id: user_id)
second query
Article.where.not(user_id: user_id)
and merge result
second Idea
get all articles and select method in ruby
but i need best way make this
i use Ruby On Rails 6.1 and Ruby 3
You could run one query but sort the articles with SQL depending on if they have a matching user_id:
Article.order(Arel.sql("CASE user_id WHEN #{user_id} THEN 0 ELSE 1 END"))
Note: order does not support multiple arguments and input sanitization out of the box. Use this only, when you are sure that the user_id contains only a valid user id, for example, be using current_user.id instead of user_id
In Rails 7 there will be a new method called in_order_of which would allow writing the same functionality like this:
Article.in_order_of(:user_id, user_id)
More programmatic approach
articles = Article.arel_table
case_statement = Arel::Nodes::Case.new(articles[:user_id])
.when(user_id)
.then(0)
.else(1)
Article.order(case_statement)

Age-based Conditional Statements in Rails 3

--still learning rails here.. I want to write a conditional statement such as:
if variable.created_at.method_for_checking_if_older_than(10 minutes)
#code
end
What statement or method can i use on a created_at value to fulfil the purpose of checking if it is older than a certain number of minutes/hours etc..?
You're looking for this:
if variable.created_at < 10.minutes.ago
#code
end
Rails adds several such methods to the standard Ruby Numeric class, a full list is available from the Rails API documentation:
http://api.rubyonrails.org/classes/Numeric.html

Best way to join unique month and year from db in rails 3 ( or otherwise )

I am trying to figure out a nice way of doing this and thought maybe there is a nicer way in the newer Rails 3.0 ActiveRecord query.
I have a bunch of Posts that have a published_at field.
Now I want to present an Archive in the sidebar with all unique months and year that contains posts and display that archive. What's the best way to do this avoiding to heavy hits on the DB on every pageload? Suggestions?
You need a query along the lines of select distinct date_format(published_at, '%m %y'), count(id) from posts group by 1. It's a trivial matter to convert this to AR syntax.
RE: pageload
Run the query for the archive and cache the result using either query caching or fragment caching.

activerecord equivalent to SQL 'minus'

What's the rails way to subtract a query result from another? A database specific SQL example would be:
SELECT Date FROM Store_Information
MINUS
SELECT Date FROM Internet_Sales
I'll throw this into the mix - not a solution, but might help with the progress:
Best I can think of is to use NOT IN:
StoreInformation.where('date NOT IN (?)', InternetSale.all)
That's Rails 3 - Rails 2 would be:
StoreInformation.all(:conditions => ['date NOT IN(?)', InternetSale.all])
But both of these will first select everything from internet_sales; what you really want is a nested query to do the whole thing in the database engine. For this, I think you'll have to break into find_by_sql and just give a nested query.
Obviously this assumes you're using MySQL! HTH.
Late answer but I think you meant :
activities = Activity.all
searches = activites.where(key: 'search')
(activites - searches).each do |anything_but_search|
p anything_but_search
end
You can substract two ActiveRecordsRelation and get the MINUS result, just like SQL would.
I am using Rails 4.2 so anything beyond that version should do the trick.

How to Properly Convert or Query Date Range for Rails / MySQL DateTime Column

I have a rails application where I store created_at as datetime (standard).
I am building a form for searching and I find I have to use find_by_sql to do some complex subqueries. The form has a date range (no time) to search on for items created_at field.
The problem I find is that if I pass in just the date string for range to query...
... status_changes.created_at between '2009-01-24' and '2009-03-12' ...
I am getting back records that have a created_at date of 2009-01-23 17:10:39 -0800 because this is stored in the db as 2009-01-24 01:10:39 (UTC)
How can I fix this so that the result is not returning the record in question?
It seems I either need to convert the date range to be UTC specific or tell the find_by_sql to search based on current time zone instead of reading the column as utc...
Any takers?
John
The modern ActiveRecord way of doing this is:
Model.where(time_field: date1..date2)
If you don't use find_by_sql, but rather use a find with a :conditions clause that let's Rails do substitutions, it will convert everything automatically.
Model.find :all,
:conditions => ["created_at between ? and ?", start_date, end_date]
Worst case, if Rails is confused by the dates, you can convert them to times and it should play nicely:
Model.find :all,
:conditions => ["created_at between ? and ?",
start_date.to_time, end_date.to_time]
I know that this is an old question, but in answer to Streamlines query about CONVERT_TZ:
Unless you have the mySQL named timezone tables loaded (which is pretty unlikely on a vanilla install), you need to enter timezones as an offset from UTC.
CONVERT_TZ(status_changes.created_at, '+00:00', '+08:00') between '2009-01-24' and '2009-03-12')
MySQL has a CONVERT_TZ function that takes a datetime and converts it from one timezone to another. You could build your query to convert from the stored value (UTC) to your local timezone (PST).
CONVERT_TZ(status_changes.created_at,'UTC',?) between ? and ?, 'PST, '2009-01-24', '2009-03-10'
I tried Ian's answer, but it did not quite work. Rails is correctly handling the timezone when records are saved and loaded, but doesn't seem to do the same for query parameters. Note that the application I'm working on is in Rails 2.1.1, so things may have improved somewhat since then.
With one small tweak, I was able to make Ian's solution work though: Get the time value in the UTC timezone using the getutc method, and then pass that as the parameter. This should also not cause any harm in any later versions of Rails that may handle the time parameter zone correctly, since UTC time is not altered by getutc, just the timezone.
Because Rails stores the dates in UTC in the database you should use the utc method on Time or DateTime
i.e.
Model.find :all,
:conditions => ["created_at between ? and ?", start_date.utc, end_date.utc]
Model.where(:from => "2012-01-01".to_date.."2012-06-30".to_date)
worked for me ( :from is database field of the type 'date' )