Rspec, rails 7 Update_column on date value not updating the record - ruby-on-rails

after im upgrading to rails 7 and ruby 3, I'm facing one issue in rspec.
issue:
whenever I'm using the update_column of date field of a column of the model in rspec, it shows the result of the update as true, but that value is not getting updated in the database.
Example:
My model.
Dishes
it contains prepared time, and served time as date field:
structure.sql file.
CREATE TABLE public.dishes (
....
....
prepared_time date,
served_time date,
......
)
my rspec
it "should send notification on updating served time" do
dish.update_column(:prepared_date, (Time.zone.now).strftime('%m/%d/%Y'))
params['orders'].merge!(served_time: [(Time.zone.now +
3.day).strftime('%m/%d/%Y')])
response = put(:update, params)
json = JSON.parse(response.body)
expect(json).to eq(success_response)
end
here after debugging im found that the dish is not updated with prepared_date, as the prepared_date is nil, but when I try to update other fields, they are getting updated.
But the same spec works on rails 4.
what might be the reason??

Related

ActiveRecord updated_at error on rails 5

When I manually set the updated_at field of my model in a controller as follows:
el = Model.find(id)
el.update(updated_at: Time.now, updated_by: "some_name")
The minutes of the created_at field change to match those of updated_at.
This does not happen when I write the above lines of code in the console.
Any ideas ?
Edit:
Project background:
I am using Rails 5, and the mysql2 gem.
The problem does not arise when I repeat the commands in the rails console.
To preform the action, I make an ajax call to the server from the client, and the function in the controller performs the action.
Update:
The same thing happens when using:
el.update_attribute(:updated_by, "some_name")
Which should automatically handle the updated_at field.
Command Line Output:
img
I do not see the created_at field get modified anywhere.
Also, if relevant I am on Windows 7, 64 bit.
Update 2:
I pushed this project to a Heroku server and the bug does not persist.
I can only conclude this is a Windows bug.

Rails - Querying a JSON Field

In my Rails 5 app, I'm using the Ahoy gem which uses a jsonb field (called properties) that I don't know how to query.
A sample of the data in the properties field:
"{\"id\":\"11\"}"
What I'm trying to do is:
Find the records created in the last seven days (using the time field)
Group the records by the id in the properties field
Order the records based on which id had the most records
I tried this:
#foo = Ahoy::Event.where(name: "Viewed Product").where(time: 7.days.ago..Time.now).group("properties(id)").order('COUNT("properties(id)") DESC')
but I received the error, PG::UndefinedColumn: ERROR: column "properties(id)" doe not exist
I found this somewhat similar question in the issues for the gem, so I tried breaking the query into parts:
#foo = Ahoy::Event.where(name: "Viewed Product").where(time: 7.days.ago..Time.now)
#foo.group("properties REGEXP '[{,]\"id\"...).count
but then my IDE gives the error unterminated string meets the end of file.
Can someone please help me determine what I'm doing wrong?
If you are using PostgreSQL, I believe that you would have to refer to json columns like this:
.group("properties ->> 'id'")

In a Rails controller creating a new index in a 'map.do' loop fails, yet it's fine in Ruby

I have a bunch of records from a database using select id, parent_id.... and loop over them using map.do creating a new index on each row.
records = results.map do |row|
row['childs'] = {}
comments[row['id']] = row
end
This works fine when called from the command-line but when placed in the Rails context I get the following error:
>ActiveModel::MissingAttributeError in CommentsController#index
can't write unknown attribute `childs'
Any idea what this would work in Ruby at the command line and not Rails? What am I missing about how Rails works?
row is not a hash, but an ActiveModel object instance, so you can't just add/assign new attributes to it like a key to the hash. In your model, you can use attr_accessor to initiate setters(writing) and getters(reading) for childs attribute.

Get hour from user and store in rails model + timezone

I have a rails view where the user is presented with a dropdown displaying the hours '00:00', '01:00' etc.
Once the user has selected this hour, then it is stored in the model with some other fields.
My application is working in timezone UTC+2 (I have set config.time_zone = 'Jerusalem').
At what stage to I related to timezones when handling this time on its path from the view to the model? If I use e.g. DateTime.strptime("10:00", "%H:%M").in_time_zone(Time.zone), then I get a time at '12:00' - while what I really would like is for the database to store in UTF (which should be 08:00 while the rest of the site works with local time.
You can use TZInfo ruby library http://tzinfo.rubyforge.org and convert your time with following method
def local_to_utc(date, time_zone)
begin
unless time_zone.nil?
tz = TZInfo::Timezone.get(time_zone)
return tz.local_to_utc(date)
end
rescue
return nil
end
end

How do you test dates in ruby on rails 3 with rspec?

Say for instance I have a datefield or 3 select fields for day month and yea..
How does the final selection of date get sent to the database? or in what format doesn't it get sent?
20110803 ?
2011-08-03 ?
What I want to do is:
Test for a valid date (a date that has been selected that exists)
Test for an invalid date (invalid would be a date that doesn't exist and also be when nothing has been selected)
I know an idea of how to write these tests but not sure what actually gets sent to the database
What actually appears before save and in what format. String? Integer?
Thanks in advance for responses.
The gets sent to the database as the database expects it. Each database and configuration will yield a different format for a date but this will not make any difference to you as the field is configured to be Date and it will also be a Date on Rails, so you should not care about the format.
While testing, there are many ways you can validate, but you will not be using neither integer or string, you're using Date objects. Here's an example of how you could write the first one:
it 'should find a model today' do
#date = Date.today
#model = Model.create!(:date => #date)
Model.first( :conditions => {:date => #date}).should == #model
end

Resources