How to update boolean value in rails - ruby-on-rails

I'm trying to update the occupied field of runn to true during the creation of a stay.
def create
if params[:id].present?
#patient = Patient.find(params[:id])
#stay = #patient.stays.build(stay_params)
#stay.is_current = true
Runn.find_by_id(#stay.runn_id).update_attribute(:occupied, true)
else
#stay = Stays.new(stay_params)
end
if #stay.save
redirect_to #patient
end
end
This is the create method in my stays_controller.rb
class Stay < ActiveRecord::Base
belongs_to :patient
has_one :runn
has_many :feeding_events
has_many :ud_events
has_many :misc_stay_events
after_create :update
private
def update
self.check_in_dt = Time.new
self.is_current = true
end
end
This is stay.rb
class Runn < ActiveRecord::Base
belongs_to :ward
belongs_to :stay
end
This is runn.rb
When I use the console to display all, they all show that occupied is false, which makes sense because that's how I set it in seeds. However, I'm wondering how to update occupied? When I do:
[1] pry(main)> Runn.find_by_id(1)
Runn Load (0.5ms) SELECT "runns".* FROM "runns" WHERE "runns"."id" = $1 LIMIT 1 [["id", 1]]
=> #<Runn:0x007f478460d6c8
id: 1,
ident: "Run 0.0",
size: "Medium,",
amenities: nil,
created_at: Thu, 03 Dec 2015 05:50:01 UTC +00:00,
updated_at: Thu, 03 Dec 2015 05:50:01 UTC +00:00,
ward_id: 1,
occupied: false>
[2] pry(main)> Runn.find_by_id(1).occupied = true
Runn Load (0.6ms) SELECT "runns".* FROM "runns" WHERE "runns"."id" = $1 LIMIT 1 [["id", 1]]
=> true
[3] pry(main)> Runn.find_by_id(1)
Runn Load (0.5ms) SELECT "runns".* FROM "runns" WHERE "runns"."id" = $1 LIMIT 1 [["id", 1]]
=> #<Runn:0x007f478467a6b0
id: 1,
ident: "Run 0.0",
size: "Medium,",
amenities: nil,
created_at: Thu, 03 Dec 2015 05:50:01 UTC +00:00,
updated_at: Thu, 03 Dec 2015 05:50:01 UTC +00:00,
ward_id: 1,
occupied: false>
[4] pry(main)>
The occupied has not been update. However, with update_attribute:
[4] pry(main)> Runn.find_by_id(1).update_attribute(:occupied, true)
Runn Load (0.4ms) SELECT "runns".* FROM "runns" WHERE "runns"."id" = $1 LIMIT 1 [["id", 1]]
(0.2ms) BEGIN
SQL (0.4ms) UPDATE "runns" SET "occupied" = $1, "updated_at" = $2 WHERE "runns"."id" = $3 [["occupied", "t"], ["updated_at", "2015-12-03 05:52:33.949456"], ["id", 1]]
(11.0ms) COMMIT
=> true
[5] pry(main)> Runn.find_by_id(1)
Runn Load (0.5ms) SELECT "runns".* FROM "runns" WHERE "runns"."id" = $1 LIMIT 1 [["id", 1]]
=> #<Runn:0x007f479d0fd098
id: 1,
ident: "Run 0.0",
size: "Medium,",
amenities: nil,
created_at: Thu, 03 Dec 2015 05:50:01 UTC +00:00,
updated_at: Thu, 03 Dec 2015 05:52:33 UTC +00:00,
ward_id: 1,
occupied: true>
[6] pry(main)> Runn.all
Runn Load (0.6ms) SELECT "runns".* FROM "runns"
=> [#<Runn:0x007f479d0aafa0
id: 2,
ident: "Run 0.1",
size: "Small,",
amenities: nil,
created_at: Thu, 03 Dec 2015 05:50:01 UTC +00:00,
updated_at: Thu, 03 Dec 2015 05:50:01 UTC +00:00,
ward_id: 1,
occupied: false>,
#<Runn:0x007f479d0aae60
id: 3,
ident: "Run 1.0",
size: "Small,",
amenities: nil,
created_at: Thu, 03 Dec 2015 05:50:01 UTC +00:00,
updated_at: Thu, 03 Dec 2015 05:50:01 UTC +00:00,
ward_id: 2,
occupied: false>,
#<Runn:0x007f479d0aad20
id: 4,
ident: "Run 1.1",
size: "Medium,",
amenities: nil,
It has been successfully updated, but it has been removed from Runn.all. Any advice?
EDIT:
[1] pry(main)> Runn.all
Runn Load (0.8ms) SELECT "runns".* FROM "runns"
=> [#<Runn:0x007f4784e9b3a8
id: 1,
ident: "Run 0.0",
size: "Medium,",
amenities: nil,
created_at: Thu, 03 Dec 2015 05:59:02 UTC +00:00,
updated_at: Thu, 03 Dec 2015 05:59:02 UTC +00:00,
ward_id: 1,
occupied: false>,
#<Runn:0x007f4784e98a40
id: 2,
ident: "Run 0.1",
size: "Small,",
amenities: nil,
created_at: Thu, 03 Dec 2015 05:59:02 UTC +00:00,
updated_at: Thu, 03 Dec 2015 05:59:02 UTC +00:00,
ward_id: 1,
occupied: false>,
#<Runn:0x007f4784e98900
id: 3,
ident: "Run 1.0",
size: "Small,",
amenities: nil,
original Runn.all.

Please try this to update:
runn = Runn.find_by_id(1)
if !runn.nil?
runn.occupied = true
runn.save
end

Related

Testing for FactoryGirl Results

I'm trying to test to see if the items the array exist after I create the factory.
spec/models/thing_spec.rb
require 'rails_helper'
RSpec.describe Thing, :type => :model do
let(:thing) { Array.new(3) {FactoryGirl.create(:thing) } }
it "should sort the items in order" do
expect(thing).to include(ordering:1, ordering:2, ordering:3)
end
end
spec/factories/things.rb
FactoryGirl.define do
factory :thing, :class => 'Thing' do
name "item_name"
sequence(:ordering)
end
end
Below are the results that I received.
results
1) Things should be sorted in order
Failure/Error: expect(thing).to include(ordering:1, ordering:2, ordering:3)
expected [#<Thing id: 1, name: "item_name", create_date: "2014-11-07 04:18:17", modified_date: "2014-11-14 04:18:17", ordering: 1>, #<Thing id: 2, name: "item_name", create_date: "2014-11-07 04:18:17", modified_date: "2014-11-14 04:18:17", ordering: 2>, #<Thing id: 3, name: "item_name", create_date: "2014-11-07 04:18:17", modified_date: "2014-11-14 04:18:17", ordering: 3>] to include {:ordering => 2}
Diff:
## -1,2 +1,19 ##
-[{:ordering=>2}]
+[#<Thing:0x007fb96217cc30
+ id: 1,
+ name: "item_name",
+ create_date: Fri, 07 Nov 2014 04:18:17 UTC +00:00,
+ modified_date: Fri, 14 Nov 2014 04:18:17 UTC +00:00,
+ ordering: 1>,
+ #<Thing:0x007fb9621cfca0
+ id: 2,
+ name: "item_name",
+ create_date: Fri, 07 Nov 2014 04:18:17 UTC +00:00,
+ modified_date: Fri, 14 Nov 2014 04:18:17 UTC +00:00,
+ ordering: 2>,
+ #<Thing:0x007fb96221eda0
+ id: 3,
+ name: "item_name",
+ create_date: Fri, 07 Nov 2014 04:18:17 UTC +00:00,
+ modified_date: Fri, 14 Nov 2014 04:18:17 UTC +00:00,
+ ordering: 3>]
You can't do it this way. You'll have to check each record individually like this
it "should sort the items in order" do
expect(thing[0].ordering).to eq(1)
expect(thing[1].ordering).to eq(2)
expect(thing[2].ordering).to eq(3)
end
Or do something like this:
it "should sort the items in order" do
expect(thing.map(&:ordering)).to eq([1, 2, 3])
end
You can only use include to check if the array includes an element as a whole, like this:
expect(thing).to include(thing[0])

Postgresql database query to Ruby active record

There are tables audits and audit_statuses, and need to fetch the data which are not common in both. I already have a query that works in postgresql , how to convert convert it into Ruby active record .
the following is the Psql query
SELECT a.name as status,count(b.audit_status_id) as count
FROM audit_statuses as a,audits as b
WHERE a.id=b.audit_status_id
GROUP BY a.name;
Your association models are Audit belongs_to Audit_status, and Audit_status has_many Audits, aren't there? And You want to get counts how many Audit_statuses owned by Audit, won't you?
If yes, you should use counter_cache read this about belongs_to Association Reference #counter_cache
Although the :counter_cache option is specified on the model that
includes the belongs_to declaration, the actual column must be added
to the associated model. In the case above, you would need to add a
column named count_audit to the Audit_status model
Add count_audit attribute to audit_statuses table by running :
rails g migration AddCountAuditToAuditStatuses count_audit:integer
and run rake db:migrate
On your models looks like :
class Audit < ActiveRecord::Base
attr_accessible :audit_status_id, :status
belongs_to :audit_status, :counter_cache => :count_audit
end
class AuditStatus < ActiveRecord::Base
attr_accessible :count_audit, :name
has_many :audits
end
Example create one record of audit_status
irb(main):001:0> audit_status = AuditStatus.create!(:name => "John Noe")
←[1m←[36m (0.0ms)←[0m ←[1mBEGIN←[0m
←[1m←[35mSQL (114.0ms)←[0m INSERT INTO "audit_statuses" ("count_audit", "created_at", "name", "up
dated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["count_audit", nil], ["created_at", Fri, 06 Jun
2014 15:17:49 WIB +07:00], ["name", "John Noe"], ["updated_at", Fri, 06 Jun 2014 15:17:49 WIB +07:0
0]]
←[1m←[36m (17.0ms)←[0m ←[1mCOMMIT←[0m
=> #<AuditStatus id: 1, name: "John Noe", count_audit: nil, created_at: "2014-06-06 08:17:49", updat
ed_at: "2014-06-06 08:17:49">
And create two records of audit, and get audit_status.id for audit_status_id
irb(main):002:0> audit = Audit.create!({:audit_status_id => audit_status.id, :status => true})
←[1m←[35m (0.0ms)←[0m BEGIN
←[1m←[36mSQL (6.0ms)←[0m ←[1mINSERT INTO "audits" ("audit_status_id", "created_at", "status", "up
dated_at") VALUES ($1, $2, $3, $4) RETURNING "id"←[0m [["audit_status_id", 1], ["created_at", Fri,
06 Jun 2014 15:19:00 WIB +07:00], ["status", true], ["updated_at", Fri, 06 Jun 2014 15:19:00 WIB +07
:00]]
←[1m←[35mAuditStatus Load (1.0ms)←[0m SELECT "audit_statuses".* FROM "audit_statuses" WHERE "audi
t_statuses"."id" = 1 LIMIT 1
←[1m←[36mSQL (2.0ms)←[0m ←[1mUPDATE "audit_statuses" SET "count_audit" = COALESCE("count_audit",
0) + 1 WHERE "audit_statuses"."id" = 1←[0m
←[1m←[35m (1.0ms)←[0m COMMIT
=> #<Audit id: 1, audit_status_id: 1, status: true, created_at: "2014-06-06 08:19:00", updated_at: "
2014-06-06 08:19:00">
irb(main):003:0> audit = Audit.create!({:audit_status_id => audit_status.id, :status => false})
←[1m←[36m (1.0ms)←[0m ←[1mBEGIN←[0m
←[1m←[35mSQL (1.0ms)←[0m INSERT INTO "audits" ("audit_status_id", "created_at", "status", "update
d_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["audit_status_id", 1], ["created_at", Fri, 06 Jun 2
014 15:19:23 WIB +07:00], ["status", false], ["updated_at", Fri, 06 Jun 2014 15:19:23 WIB +07:00]]
←[1m←[36mAuditStatus Load (1.0ms)←[0m ←[1mSELECT "audit_statuses".* FROM "audit_statuses" WHERE "
audit_statuses"."id" = 1 LIMIT 1←[0m
←[1m←[35mSQL (0.0ms)←[0m UPDATE "audit_statuses" SET "count_audit" = COALESCE("count_audit", 0) +
1 WHERE "audit_statuses"."id" = 1
←[1m←[36m (0.0ms)←[0m ←[1mCOMMIT←[0m
=> #<Audit id: 2, audit_status_id: 1, status: false, created_at: "2014-06-06 08:19:23", updated_at:
"2014-06-06 08:19:23">
So, you only call audit_status's records looks like :
irb(main):004:0> #audit_statuses = AuditStatus.all
←[1m←[35mAuditStatus Load (1.0ms)←[0m SELECT "audit_statuses".* FROM "audit_statuses"
=> [#<AuditStatus id: 1, name: "John Noe", count_audit: 2, created_at: "2014-06-06 08:17:49", update
d_at: "2014-06-06 08:17:49">]
#audit_status = Audit.joins( :audit_status).select("name as status,count(audit_status_id) as count").group(:name)

has_one, dependent: destroy not working

I am using Devise for my user authentication and would like to destroy an associated profile along with the user.
My failing spec looks like this:
it "should destroy associated profile" do
profile = #user.profile
#user.destroy
expect(profile).to be_nil
end
And
In my user model:
has_one :profile, dependent: :destroy
In my profile model:
belongs_to :user
In the console, I can reproduce the issue like this:
2.0.0p247 :001 > #user = FactoryGirl.create(:user)
(1.5ms) BEGIN
User Exists (2.9ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = 'person946979#example.com' LIMIT 1
User Exists (1.7ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('person946979#example.com') LIMIT 1
SQL (15.7ms) INSERT INTO "users" ("created_at", "email", "encrypted_password", "name", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["created_at", Fri, 16 Aug 2013 01:21:12 UTC +00:00], ["email", "person946979#example.com"], ["encrypted_password", "$2a$10$0704XOlw.6ZE4HEfDhaIeuwnEbbJZvZda3Jwr052aLS5z3G77Dgja"], ["name", "Example User"], ["updated_at", Fri, 16 Aug 2013 01:21:12 UTC +00:00]]
SQL (3.8ms) INSERT INTO "profiles" ("created_at", "updated_at", "user_id") VALUES ($1, $2, $3) RETURNING "id" [["created_at", Fri, 16 Aug 2013 01:21:12 UTC +00:00], ["updated_at", Fri, 16 Aug 2013 01:21:12 UTC +00:00], ["user_id", 25]]
Profile Load (3.4ms) SELECT "profiles".* FROM "profiles" WHERE "profiles"."user_id" = $1 ORDER BY "profiles"."id" ASC LIMIT 1 [["user_id", 25]]
(2.2ms) COMMIT
=> #<User id: 25, email: "person946979#example.com", encrypted_password: "$2a$10$0704XOlw.6ZE4HEfDhaIeuwnEbbJZvZda3Jwr052aLS5...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2013-08-16 01:21:12", updated_at: "2013-08-16 01:21:12", name: "Example User">
2.0.0p247 :002 > #user.destroy
(1.0ms) BEGIN
SQL (2.5ms) DELETE FROM "profiles" WHERE "profiles"."id" = $1 [["id", 4]]
SQL (5.4ms) DELETE FROM "users" WHERE "users"."id" = $1 [["id", 25]]
(2.0ms) COMMIT
=> #<User id: 25, email: "person946979#example.com", encrypted_password: "$2a$10$0704XOlw.6ZE4HEfDhaIeuwnEbbJZvZda3Jwr052aLS5...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: "2013-08-16 01:21:12", updated_at: "2013-08-16 01:21:12", name: "Example User">
Interestingly, the user appears to actually have been deleted.
2.0.0p247 :003 > #user.reload.destroy
User Load (2.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 25]]
ActiveRecord::RecordNotFound: Couldn't find User with id=25
What is going on here?
Your model seems fine. Try something like this:
it "should destroy associated profile" do
profile = #user.profile
#user.destroy
expect(Profile.find(profile.id)).to be_nil
end
Like Heungju said, while the database row that corresponds to profile is being destroyed, the variable itself isn't.
How about this?
it "should destroy associated profile" do
profile = #user.profile
#user.destroy
expect(#user.profile).to be_nil
end
After #user.destroy, the 'profile', variable that expected to be nil, was not changed. I think...
Rewriting my spec like this does what I need:
it "should destroy associated profile" do
expect {
#user.destroy
}.to change(Profile, :count).by(-1)
end

Why does this where clause not return the right record? - Rails 3

=> #<Gig id: 59, date: "2012-06-01 00:00:00", title: "Awesome Record", url: "http://someurl.com", body: nil, reply_email: nil, industry_id: nil, created_at: "2012-06-03 03:06:45", updated_at: "2012-06-03 03:06:45">
1.9.2-p0 :046 > Gig.where(:date => "2012-06-01 00:00:00")
Gig Load (0.3ms) SELECT "gigs".* FROM "gigs" WHERE "gigs"."date" = '2012-06-01 00:00:00'
=> []
1.9.2-p0 :043 > Gig.where(:date => "2012-06-01")
Gig Load (0.3ms) SELECT "gigs".* FROM "gigs" WHERE "gigs"."date" = '2012-06-01'
=> []
1.9.2-p0 :044 > Gig.where(:date => "June 01")
Gig Load (0.6ms) SELECT "gigs".* FROM "gigs" WHERE "gigs"."date" = 'June 01'
=> []
1.9.2-p0 :045 > Gig.where(:date => "June 1")
Gig Load (32.9ms) SELECT "gigs".* FROM "gigs" WHERE "gigs"."date" = 'June 1'
=> []
All I am trying to do is find the records in this table that have a specific 'date' (or even records that fall within a date range). But the basic where clause above won't work.
What am I doing wrong?
Edit 1
I outputted the date as string, via to_s and this is the output:
d.date
=> Fri, 01 Jun 2012 00:00:00 UTC +00:00
1.9.2-p0 :079 > d.date.to_s
=> "2012-06-01 00:00:00 UTC"
Not sure if that will help....but I even tried including those strings as the condition of the where clause and it still returns an empty array.
Try these:
Gig.where(:date => "2012-06-01 00:00:00 +00.00")
Gig.where("date like '2012-06-01%'")
Gig.where("DATE(date) = '2012-06-01'")
Give a shot:
Gig.where('date = ?', '2012-06-01 00:00:00')

Time not updating in heroku

I'm seriously confused what could be going wrong.
I'm logged into heroku run console and trying to update a timestamp on my database.
I've run User.find(6) to see that the user has a :next_click = 2000-01-01... i don't know why it's that value, but anyway, I do User.update 6, {:next_click => Time.utc(2015)} and it seems to update properly saying 2015-01-01 00:00:00, however when I do another User.find(6) it seems the time has switched back because it's not 2015-01-01 00:00:00.
I'm really confused why it's not. Any insight?
SEE ATTACHED SCREENSHOT
irb(main):033:0> User.update 6, {:next_click => Time.utc(2015) }
User Load (34.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 6]]
(1.8ms) BEGIN
(2.2ms) UPDATE "users" SET "next_click" = '2015-01-01 00:00:00.000000', "updated_at" = '2012-05-24 00:13:26.197358' WHERE "users"."id" = 6
(2.2ms) COMMIT
=> #<User id: 6, name: "mazlix", gold: 10, points: 10, next_click: "2015-01-01 00:00:00", created_at: "2012-05-23 23:40:39", updated_at: "2012-05-24 00:13:26">
irb(main):034:0> User.find(6)
User Load (2.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 6]]
=> #<User id: 6, name: "mazlix", gold: 10, points: 10, next_click: "2000-01-01 00:00:00", created_at: "2012-05-23 23:40:39", updated_at: "2012-05-24 00:13:26">
The same thing happens with u = User.find(6) u.next_click = Time.utc(2013) then u.save
irb(main):001:0> u = User.find(6)
User Load (38.8ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 6]]
=> #<User id: 6, name: "mazlix", gold: 10, points: 10, next_click: "2000-01-01 00:00:00", created_at: "2012-05-23 23:40:39", updated_at: "2012-05-24 00:57:28">
irb(main):002:0> u.next_click = Time.utc(2013)
=> 2013-01-01 00:00:00 UTC
irb(main):003:0> u
=> #<User id: 6, name: "mazlix", gold: 10, points: 10, next_click: "2013-01-01 00:00:00", created_at: "2012-05-23 23:40:39", updated_at: "2012-05-24 00:57:28">
irb(main):004:0> u.save
(10.9ms) BEGIN
(3.7ms) UPDATE "users" SET "next_click" = '2013-01-01 00:00:00.000000', "updated_at" = '2012-05-24 03:05:46.059530' WHERE "users"."id" = 6
(2.2ms) COMMIT
=> true
irb(main):005:0> u
=> #<User id: 6, name: "mazlix", gold: 10, points: 10, next_click: "2013-01-01 00:00:00", created_at: "2012-05-23 23:40:39", updated_at: "2012-05-24 03:05:46">
irb(main):006:0> User.find(6)
User Load (33.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 6]]
=> #<User id: 6, name: "mazlix", gold: 10, points: 10, next_click: "2000-01-01 00:00:00", created_at: "2012-05-23 23:40:39", updated_at: "2012-05-24 03:05:46">
app/models/user.rb:
class User < ActiveRecord::Base
attr_accessible :gold, :name, :next_click, :points
end
You've created a time column. Slightly confusingly, even though in ruby Time stores time + date, in the database world the concept of a pure time of day (ie just hours/minutes/seconds) exists and that's what you get when you add a column of type :time to your migration.
Ruby itself doesn't have a class that represents a time without a date so rails uses an instance of Time but ignores the date bit.
If you want a column that stores both time and date, change the column to a :datetime one (you'll still get a Time instance back in most cases.
In your ActiveRecord can use the data type :timestamp or :datetime to store the correct date format.

Resources