rails changed? method is always false - ruby-on-rails

I am trying to check in my model if a checkbox value was changed or not. If it was changed I want my method set_ip_setting to run in before_save but my x variable is always coming back as false even if I change the value. Why is it coming back as always false? I don't see what I am doing wrong
before_save :set_ip_setting, if: :check_my_ip
def check_my_ip
x = self.sip.my_ip_changed?
is_ip = self.sip.my_ip
if x == true && is_ip == true
return false
elsif x == false && is_ip == true
return false
elsif x == true
return true
end
end
def set_ip_setting
if self.sip.try(:my_ip) == true
sip.nat = "yes"
sip.invite = "yes"
end
end
EDITED
I got my desired outcome by using previous_changes method but I think there is a cleaner way but here is what worked out for me
def check_my_ip
x = self.sip.previous_changes[:my_ip]
off_to_on = [false, true]
on_to_off = [true, false]
is_ip = self.sip.my_ip ? 1 : 0
if x.nil? && is_ip == 1
return false
elsif x == on_to_off
return false
end
return true
end

Generally you do not write if x == true. In Ruby, true and false are distinct values. For example, 1 == true is false.
Instead, simply check if x or if !x.
def check_my_ip
# Note that an explicit `self` is not necessary except when setting a value.
x = sip.my_ip_changed?
is_ip = sip.my_ip
if x && is_ip
return false
elsif !x && is_ip
return false
elsif x
return true
end
end
def set_ip_setting
if self.sip.try(:my_ip)
sip.nat = "yes"
sip.invite = "yes"
end
end
Your logic can be simplified, and implicit return used.
def check_my_ip
x = sip.my_ip_changed?
is_ip = sip.my_ip
if is_ip
false
elsif x
true
else
false
end
end

Related

Lua function constantly returning false out of SQL function

I have this code
function IsAuthorized(xPlayer, doorID, locked, usedLockpick)
local jobName, grade = {}, {}
jobName[1] = xPlayer.job.name
grade[1] = xPlayer.job.grade
if xPlayer.job2 then
jobName[2] = xPlayer.job2.name
grade[2] = xPlayer.job2.grade
end
local canOpen = false
if doorID.lockpick and usedLockpick then
count = xPlayer.getInventoryItem('lockpick').count
if count and count >= 1 then canOpen = true end
end
if not canOpen and doorID.authorizedJobs then
for job,rank in pairs(doorID.authorizedJobs) do
if (job == jobName[1] and rank <= grade[1]) or (jobName[2] and job == jobName[2] and rank <= grade[2]) then
canOpen = true
if canOpen then break end
end
end
end
if not canOpen and doorID.items then
local count
for k,v in pairs(doorID.items) do
count = xPlayer.getInventoryItem(v).count
if count and count >= 1 then
canOpen = true
local consumables = { ['ticket']=1 }
if locked and consumables[v] then
xPlayer.removeInventoryItem(v, 1)
end
break
end
end
if not count or count < 1 then canOpen = false end
end
if not canOpen then
local group = xPlayer.getGroup()
if group == 'management' or group == 'owner' then
print(group..' '..xPlayer.getName()..' was authorised to use a door')
canOpen = true
end
if doorID.authorizedgang then
print(xPlayer.identifier .. " | " .. doorID.authorizedgang)
MySQL.Async.fetchSingle('SELECT * FROM users WHERE identifier = ? AND gang = ?', {xPlayer.identifier, doorID.authorizedgang}, function(foundplayer)
if foundplayer then
print("found")
canOpen = true
print(canOpen)
end
----------------------------canopen returns true until here
end)
end
end
print(canOpen)
return canOpen
end
Essentially what is does is return a true or false value based on the if statements. I did not write this script myself, I simply added on. The code that I added to this function is
if doorID.authorizedgang then
print(xPlayer.identifier .. " | " .. doorID.authorizedgang)
MySQL.Async.fetchSingle('SELECT * FROM users WHERE identifier = ? AND gang = ?', {xPlayer.identifier, doorID.authorizedgang}, function(foundplayer)
if foundplayer then
print("found")
canOpen = true
print(canOpen)
end
----------------------------canopen returns true until here
end)
end
This code essentially makes a database call to check if it'll find a player with X identifier and Y gang, and this actually does work because it prints out "found" and canOpen returns true until the line that I marked, after this it returns false at the bottom of the function.
I've been trying other ways to get the data but ultimately I need the SQL DB call for this and im not sure why the variable isnt setting when in fact the database returns found. Any ideas what I could be doing wrong here?

Rails logic not firing on `false` results

Receiving as an API an array of hashes
#request['clients'].each do |client|
validations are being executed on each clientattributes. However, rails logic is failing to fire up on false statements and this ignoring them. Example validation:
def client_type_ok
if #this_client['type'] == "ADT" || #this_client['type'] == "CHD"
true
else
false
#error_code_39 = true
end
end
The controller action wants to execute only when true conditions are met:
if client_type_ok && client_type_ok
However Rails.logger is clearly confirming that this condition is being passed through although false.
Rails.logger.info !#this_client['type'].blank?
Rails.logger.info #this_client['type']
Rails.logger.info "not"
Rails.logger.info #this_client['type'] != "ADT"
Rails.logger.info "ADT"
Rails.logger.info #this_client['type'] == "ADT"
is returning
true
APT
not
true
ADT
` `
The bottom is generated as a blank. The same occurs replacing Rails.logger with p. All logic of this action is ignoring false results. While I can attempt to devise processing cases of fully true cases, this is inconvenient and counter-intuitive.
Thus, there appears to be a meta function which is impeding the handling of false cases. How can this be tracked down? Can Rails.logger logic be step traced?
You're not returning false there
def client_type_ok
if #this_client['type'] == "ADT" || #this_client['type'] == "CHD"
true
else
false # this is not doing anything. Simply ignored.
#error_code_39 = true # result of this assignment will be true.
# and it also is the last expression of the if
# so it becomes the implicit return value of the method
end
end
def client_type_ok
if #this_client['type'] == "ADT" || #this_client['type'] == "CHD"
true
else
false
#error_code_39 = true
end
end
As Sergio mentiond in the above answer,The return value of yur method will be true for the else condtion. You need to swap the places or You can rewrite the above method
def client_type_ok
return true if %w(ADT CHD).include?(#this_client['type'])
#error_code_39 = true
false
end

im having troubles getting my toggle script working in roblox

i need help
this is the script:
script.Parent.Parent.Activated:connect(function()
local a = game.Workspace.LogRideToggled.Value
if a == true then
a = false
script.Parent.Click:Play()
end
if a == false then
a = true
script.Parent.Click:Play()
end
end)
this is the hirachy:
https://imgur.com/a/4FXHY
but NOTHING happens, no errors either, except for the click sound playing
i seriously need help
The problem is is that after you do a == true, you set a to false, which a == false then matches after.
You can solve this with an if then else end statement, like so:
script.Parent.Parent.Activated:connect(function()
local a = game.Workspace.LogRideToggled.Value
if a == true then
a = false
script.Parent.Click:Play()
else
a = true
script.Parent.Click:Play()
end
end)
However, this will only change the local value of a, which means that it will not save the change.
To fix this, we need to assign the game.Workspace.LogRideToggleds value of Value directly, which we can do like:
script.Parent.Parent.Activated:connect(function()
if game.Workspace.LogRideToggled.Value == true then
game.Workspace.LogRideToggled.Value = false
script.Parent.Click:Play()
else
game.Workspace.LogRideToggled.Value = true
script.Parent.Click:Play()
end
end)
Although it's bad practice to repeatedly index this like this, so we can store game.Workspace.LogRideToggled in a local variable. You can read up on why this works but storing value doesn't here
script.Parent.Parent.Activated:connect(function()
local logRideToggled = game.Workspace.LogRideToggled
if logRideToggled.Value == true then
logRideToggled.Value = false
script.Parent.Click:Play()
else
logRideToggled.Value = true
script.Parent.Click:Play()
end
end)
Also, the == true is redundant, as Lua expects a truthy or falsey value as the condition, all == true does in this case is give true or false if it's true of false.
script.Parent.Parent.Activated:connect(function()
local logRideToggled = game.Workspace.LogRideToggled
if logRideToggled.Value then
logRideToggled.Value = false
script.Parent.Click:Play()
else
logRideToggled.Value = true
script.Parent.Click:Play()
end
end)
Yet we can clean this up a bit more, as we use script.Parent.Click:Play() in both cases, and we can replace logRideToggled.Value = with a logical not, like so.
script.Parent.Parent.Activated:connect(function()
local logRideToggled = game.Workspace.LogRideToggled
if logRideToggled.Value then
-- Todo if truthy
else
-- Todo if falsey
end
logRideToggled.Value = not logRideToggled.Value
script.Parent.Click:Play()
end)
But if you only want to toggle this value, not do anything special for either case, we can remove that entire conditional, leaving:
script.Parent.Parent.Activated:connect(function()
local logRideToggled = game.Workspace.LogRideToggled
logRideToggled.Value = not logRideToggled.Value
script.Parent.Click:Play()
end)
Hope this has helped!

Proper identification of admin for admin_data on Heroku

We have everything installed correctly, but when an admin goes to /admin_data it throws "not authorized".
heres the relevant code in config/initializers/admin_data.rb
AdminData.config do |config|
config.is_allowed_to_view = lambda {|controller| return true if current_user.admin = true }
config.is_allowed_to_update = lambda {|controller| return true if current_user.admin = true }
end
Could be the condition you're using to check equality:
if current_user.admin = true #will always be true
vs
if current_user.admin == true #will check the equality of being true
You might consider just:
if current_user.admin
since nil or false will be NOT == true

attempt to call global 'getDirectionTo' <a nil value> stack traceback

What is wrong here?
attempt to call global 'getDirectionTo' <a nil value> stack traceback: gateofexp_closed.lua:43: in gateofexp_closed.lua:5
gateofexp_closed.lua
function onUse(cid, item, frompos, item2, topos)
local isLevelDoor = (item.actionid >= 1001 and item.actionid <= 1999)
local isVocationDoor = (item.actionid >= 2001 and item.actionid <= 2008)
if not(isLevelDoor or isVocationDoor) then
-- Make it a normal door
doTransformItem(item.uid, item.itemid+1)
return true
end
local canEnter = true
if(isLevelDoor and getPlayerLevel(cid) < (item.actionid-1000)) then
canEnter = false
end
if(isVocationDoor) then
local doorVoc = item.actionid-2000
if (doorVoc == 1 and not(isSorcerer(cid))) or
(doorVoc == 2 and not(isDruid(cid))) or
(doorVoc == 3 and not(isPaladin(cid))) or
(doorVoc == 4 and not(isKnight(cid))) or
(doorVoc ~= getPlayerVocation(cid)) then
canEnter = false
end
end
if(not canEnter and getPlayerAccess(cid) == 0) then
doPlayerSendTextMessage(cid, MESSAGE_INFO_DESCR, "Only the worthy may pass.")
return true
end
doTransformItem(item.uid, item.itemid+1)
local canGo = (queryTileAddThing(cid, frompos, bit.bor(2, 4)) == RETURNVALUE_NOERROR) --Veryfies if the player can go, ignoring blocking things
if not(canGo) then
return false
end
local dir = getDirectionTo(getPlayerPosition(cid), frompos)
doMoveCreature(cid, dir)
return true
end
getDirectionTo is not defined when that code is run. This is what the message means. I don't know why this happens, of course.

Resources