Can't grab a day from datetime in Rails view - ruby-on-rails

Basically, what work in rails console doesn't seem to work in my view - I have a workshop model with column "start_date" of datetime type.
I have created an object of this kind and when I enter rails console
Workshop.find(<id>).start_date.day
returns specific day as expected (e.g. '21' in case of datetime 'Fri, 21 Apr 2017 17:00:00 UTC +00:00').
However, in my view I am not able to use 'day' method. I'm using Slim gem and this code in my show view for workshop:
p= workshop.start_date.day
results in this error: undefined method `day' for nil:NilClass
But code
p= workshop.start_date
displays the whole date properly and code
p= workshop.start_date.class
displays that the class of the object is: ActiveSupport::TimeWithZone
What is happening, why do Rails say that the class is nil, how to make it work?

Please use the following in views,
p= workshop.start_date.day if workshop.start_date
I guess some record has the start_date attribute nil and that is the reason the views throw error.

Related

How to add text method to date collection select in Ruby on Rails?

I have an instance variable named #times. This saves many datetime objects, from which I would like to select one, as expected. I used the select tag like this:
<%= form.select(:time, #times, {},{ :class => 'admin_select' })%>
But it shows every day in the following format:
2021-07-20 08:00:00 UTC
I tried using the map method, but it modifies the object. I would like to only show the times of the objects like if they had applied the strftime("%H:%M") method. Like 8:00, 8:15, 8:30, 8:45, 9:00
I know that that´s possible with the collection_select helper, but I can´t find the way to do it.

Can I apply methods to a model.where condition to get all object created in specific month?

I am trying to apply the .where function on a model of mine called news to get all news published(represented by DateTime) in a certain month.
I've tried to get the following code snippet to work:
news.where(:publish_time.strftime('%B %Y') => "month year")
But there does not seem to be support for such a call as :publish_time is a symbol rather than value.
Is there any other way to do this or should I create a column that contains the month & year of publishing?
I've found other sources that claim you should create a span of DateTime and check if the object is within that span. That does, however, feel extremely DRY-prone and not open for additions.
Add this in your News model:
scope :published_in_month, ->(datetime) { where(publish_time: datetime.beginning_of_month..datetime.end_of_month) }
Then you can do:
News.published_in_month(some_datetime)
ie:
News.published_in_month(Datetime.new(2019, 06, 01))
You could also use by_star gem to easily query date / time ranges in Active Record.
News.by_month(Date.today)

Date conversion is not working as expected, time always remains after conversion

I'm trying to do the most simple of things, convert a date to a different date format but I can't seem to do it as my knowledge in ruby is practically zero.
I tried to read through this and tried all of the possible .to_formatted_s() versions but none of them works. The time seems to always remain there no matter what. Since this date doesn't come from a date.new() but rather an existing object property I'm guessing this is the reason why .to_formatted_s() isn't working as in the example in the docs. I could be horribly wrong though.
How do I convert this date format:
2015-07-01 01:59:59 +0200
To this:
2015-07-01
My attempts were:
// inv.end is "2015-07-01 01:59:59 +0200"
inv.end.to_formatted_s(:db)
inv.end.to_s(:db)
inv.end.to_formatted_s(:iso8601)
Which all outputted 2015-06-30 23:59:59.
What am I doing wrong?
require 'date'
DateTime.parse("2015-07-01 01:59:59 +0200").strftime('%Y-%m-%d')
http://apidock.com/ruby/DateTime/strftime
The above answer is correct.If you want to perform the same date format repetitively , then create a file in initializers & write the below code:
class ActiveSupport::TimeWithZone
def my_format(options = {})
strftime('%m-%d-%Y')
end
end
& after the datetime object just write my_format like #user.updated_at.my_format

In Rails 4 How can I display info from a database based on today's date?

I am building an app using Rails 4 and postgresql and at the top of the home page I want to show specific content from the database which changes based on the date. My database is set up as follows:
id|question |show_month|show_day
01|"question to display on January 1"|01 |01
02|"question to display on January 2"|01 |02
03|"question to display on January 3"|01 |03
and so on.
Based on the suggestion made by Frederick, my code now looks like this:
On my questions_controller:
def index
#daily = Question.find_by(show_month: Time.now.month, show_day: Time.now.day)
end
On my View:
<%= #daily.question %>
I am getting the following error when I try to load my view. It is calling the error on what I have in my view, above.
"NoMethodError in Questions#index" undefined method `question' for nil:NilClass
clearly I am not calling it on my view correctly. Any suggestions on what it should be so that it displays the "question" string from my database?
I was able to get it to display a list of all questions by having #questions = Question.all in my controller and a questions.each do loop on my view, but now I need just one.
UPDATE: The error was caused because I was storing the month/day info as a string. I made them integers and re-ran my seeds and it works perfectly now.
If you're just interested in day/month without year then the easiest may be to replace your show_date column with a show_month and show_day column and store the month/day components in those columns
Your query would then be
Content.find_by(show_month: Time.now.month, show_day: Time.now.day)

Time display format problem

My object model has a attribute named "start_at" which is sotred in PostgreSQL database.
Inside the database table, the type of "start_at" is "time without time zone" and one object start_at value is e.g. 10:20:00.
However, when I render this time value to my index.html.haml view by using
%td =myobj.start_at, the view page always show "Sat Jan 01 10:20:00 UTC 2000" instead of 10:20:00.
How to get rid of it to show only 10:20:00 instead of the long string?
What about:
myobj.start_at.strftime('%H:%M:%S')
Check resource here: http://www.ruby-doc.org/core/classes/Time.html#M000392
use timeobj.strftime("%H:%M:%S")
you can view all the options for strftime here

Resources