Rails 3.2.8 - How do I get the week number from Rails?
Use strftime
:
%U
- Week number of the year. The week starts with Sunday. (00..53)
%W
- Week number of the year. The week starts with Monday. (00..53)
Time.now.strftime("%U").to_i # 43
# Or...
Date.today.strftime("%U").to_i # 43
If you want to add 43 weeks (or days,years,minutes, etc...) to a date, you can use 43.weeks
, provided by ActiveSupport:
irb(main):001:0> 43.weeks
=> 301 days
irb(main):002:0> Date.today + 43.weeks
=> Thu, 22 Aug 2013
irb(main):003:0> Date.today + 10.days
=> Sun, 04 Nov 2012
irb(main):004:0> Date.today + 1.years # or 1.year
=> Fri, 25 Oct 2013
irb(main):005:0> Date.today + 5.months
=> Mon, 25 Mar 2013
Get calendar week numbers in requested month in RoR
If I understand, you've almost done!
the last is to calculate first and last dates for month, isn't it?
So something like next may work (not tested)
def get_weeknumbers_in_month(month, year)
first_week_num = Date.new(year,month,1).strftime("%U").to_i
last_week_num = Date.new(year,month,1).end_of_month.strftime("%U").to_i
(first_week_num..last_week_num).to_a
end
Scope with days period works from the last deploy date
This is from the Rails 3.1.0 documentation:
Note that scopes defined with scope will be evaluated when they are defined, rather than when they are used. For example, the following would be incorrect:
class Post < ActiveRecord::Base
scope :recent, where('published_at >= ?', Time.now - 1.week)
end
The example above would be ‘frozen’ to the Time.now value when the Post class was defined, and so the resultant SQL query would always be the same. The correct way to do this would be via a lambda, which will re-evaluate the scope each time it is called:
class Post < ActiveRecord::Base
scope :recent, lambda { where('published_at >= ?', Time.now - 1.week) }
end
So, you can try updating your scope to use a lambda as the second argument instead of just the where
:
scope :current_availables, -> { where(date: Date.current.beginning_of_day..Date.current+14.days) }
(Not sure if you can use ->
if not do lambda
)
You can also use 14.days.from_now
instead of adding the 14 days to Date.current
.
How to limit the number of retrieved characters from a database field in rails?
Assuming you have a model Passage
with a field text
, you can select specific field (and use SQL functions within) like this:
passages = Passage.select("id, LEFT(text,10) as text_short, CHAR_LENGTH(text) as text_length")
# => [#<Passage id: 1>, #<Passage id: 2>, #<Passage id: 3>]
passages.first.id
# => 1
passages.first.text_short
# => "There is o"
passages.first.text_length
# => 453
How to pass the session variables from one rails 3.2.8 engine to another engine?
From the Rails Edge guide:
4.3.2 Using a controller provided by the application
Because Rails controllers generally share code for things like authentication and accessing session variables, by default they inherit from ApplicationController. Rails engines, however are scoped to run independently from the main application, so each engine gets a scoped ApplicationController. This namespace prevents code collisions, but often engine controllers should access methods in the main application's ApplicationController. An easy way to provide this access is to change the engine's scoped ApplicationController to inherit from the main application's ApplicationController. For our Blorgh engine this would be done by changing app/controllers/blorgh/application_controller.rb to look like:
class Blorgh::ApplicationController < ApplicationController
end
By default, the engine's controllers inherit from Blorgh::ApplicationController. So, after making this change they will have access to the main applications ApplicationController as though they were part of the main application.
This change does require that the engine is run from a Rails application that has an ApplicationController.
Related Topics
Is It a Good Idea to Purge Old Rails Migration Files
Recommended Two-Way Encryption Gems for Ruby
Duplicating a Ruby Array of Strings
How to I Add a Hyperlink to a Cell in Axlsx
How to Deal with App_Key and App_Secret (Dropbox API)
Why Is Uri.Escape() Marked as Obsolete and Where Is This Regexp::Unsafe Constant
Activeadmin Custom Views Which Retain the Activeadmin Layout
Serialize Array with Strong_Parameters
Passing Params to Cancan in Ror
Limit Number of Pages in Will_Paginate
Devise Raises Error with Rails 4.2 Upgrade
Clicking a Button with Ruby Mechanize
Create a Human-Readable List with "And" Inserted Before the Last Element from a Ruby List
Upgraded to Ruby 1.9.2 and Getting Segmentation Fault Errors in Nokogiri
Error Installing Rubymine, No Sdk Specified, But It Is Listed