Mongoid undefined method `field' for []:Array error - ruby-on-rails-3

Here's my model:
class Model
include Mongoid::Document
field :field_1, type: Integer
field :field_2, type: Integer
field :field_3, type: Integer
field :field_4, type: Integer
def success_rate
return 0 if self.field_2.nil? || self.field_2 == 0
return 0 if self.field_4.nil? || self.field_4 == 0
(1.0 * field_4/field_3) * 100
end
end
The error happens when I try to do the following:
aux = Model.where(field_1: user.id).first
However it works fine in my local machine, the issue is on my server.
What am I doing wrong?
Thanks

Have you tried using the 'in' operator and wrapping the user.id in an array like this:
['user.id']?

Related

values are passing when datatype is different in karate

When we are providing the input whose datatype is boolean passed in POST API and validating it with GET API which return value in string
eg:
* def a = 'false' // result from GET API
* def b = false //input
* match a == b
Expected Result : It should fail as the datatype is different
Actual Result ": scenario is showing PASS
Why is it passing?
whereas, I also noticed when I an validating the data from database whose column datatype is string and we are matching the data with boolean value
i.e
* match 'false' == false
Expected Result : It should fail
Actual Result : scenario is failed
I believe the issue is related to this https://github.com/intuit/karate/issues/1179
seems like it has been fixed recently. you can build the code following this https://github.com/intuit/karate/wiki/Developer-Guide#build, else it will be next release.
You can use this option to check for if else inside a feature
Scenario: if else
* def val = 'test'
# * def result = <condition> ? <true value> : <false value>
* def result = val == 'test' ? 'TRUE' : 'FALSE'
* print result

Expression with String placeholder in the filter part of JsonPath using karate

I am trying to filter my response using JSON Path where one of the condition using a value from a variable but I am not able to map variable properly, so my filter not working properly.
Sample response JSON:
{
"response":[
{
"id":"1234",
"confirmationCode":"abcd"
}
]
}
I am using the below script where I am using variable 'code':
* def Code = 'abcd'
* def value = karate.jsonPath($.response[?(#.confirmationCode == ' + Code +')])
Read the docs carefully please:
* def value = karate.jsonPath(response, "$.response[?(#.confirmationCode=='" + Code + "')]")

Lua reference table inside metatable

I have a pretty mind-bending setup right now. I have a regular function that returns a table with functions in it under keys "string" and "number":
function defGeneric()
local function funcNumber(a)
return 2*a^2
end
local function funcString(a)
return a.." - test"
end
local returnTable={}
returnTable["number"]=funcNumber
returnTable["string"]=funcString
return returnTable
end
And that works fine. But what I want to do now is make the table that this function returns callable. To illustrate, let's say we have v=defGeneric(). Specifically:
If v is called with a string str, return the result of v["string"](str)
If v is called with a number n, return the result of v["number"](n)
This is obviously a job for metatables, so I can (in my function) add the code to set a metatable:
local metaTable = {
__call = function (...) -- "call" event handler
return
end
}
setmetatable(returnTable,metaTable)
But I don't know what I would put after that return statement. I don't think I can reference returnTable, because this table will be called like so:
v=defGeneric()
v("test")
And I need to reference v's "string" function (there certainly could be multiple defGeneric() tables in one program).
I think the answer here might be some self trick but I can't wrap my head around how. How do I reference a metatable's table from the metatable?
The first argument passed to the __call function is the table it is being called on, the table returned from the function in this case. You can use type(a) to get the type of the argument as a string, so you could do something like this:
function defGeneric()
local result = {
['number'] = function(a) return 2*a^2 end,
['string'] = function(a) return a.." - test" end
}
setmetatable(result, {
__call = function(t,a)
local f = t[type(a)]
if f == nil then return "No handler for type "..type(a) end
-- alternate:
-- if f == nil and t['string'] ~= nil then return t['string'](tostring(a)) end
return f(a)
end
})
return result
end
local def = defGeneric()
print("string: "..tostring(def('sample string')))
print("number: "..tostring(def(5)))
print("table: "..tostring(def({})))
print("boolean: "..tostring(def(1 > 5)))
output
string: sample string - test
number: 50.0
table: No handler for type table
boolean: No handler for type boolean
alternate output
string: sample string - test
number: 50.0
table: table: 0x18537e0 - test
boolean: false - test

gocql - using Cassandra 3 timeuuid functions

In cqlsh I can execute something like:
SELECT id FROM names WHERE name = 'dave' AND id > maxTimeuuid('2015-04-05 00:05') AND id < minTimeuuid('2019-01-01 00:05');
... this returns a list as expected.
id
--------------------------------------
632f4960-375b-11e8-90b1-02420a000203
91f03523-3761-11e8-a777-02420a000a05
32dbffc4-3762-11e8-a778-02420a000a05
9ffb1ab8-3762-11e8-a779-02420a000a05
c28a0372-3764-11e8-a77b-02420a000a05
However I am struggling to get this to work in gocql:
var found bool = false
m := map[string]interface{}{}
qString := "SELECT id FROM names WHERE name=? AND id > maxTimeuuid(?) AND id < minTimeuuid(?);"
query := Cassandra.Sessionds.Query(qString, name, toFrom.From, toFrom.To).Consistency(gocql.One)
fmt.Println("sending IsRunning query: " + query.String())
iterable := query.Iter()
for iterable.MapScan(m) {
found = true
lookupTable = HostLookup {
Id: m["id"].(gocql.UUID),
}
}
Even though the debug line looks ok,
sending IsRunning query: [query statement="SELECT id FROM names WHERE name=? AND id > maxTimeuuid(?) AND id < minTimeuuid(?);" values=[dave 2015-04-05 00:05 2019-01-01 00:05] consistency=ONE]
... I always end up with an empty iterable. I am using very similar code elsewhere successfully, the difference here are the timeuuid functions. I've tried various arrangements with literal quotes to no avail, any advice is appreciated.
When I looked at the iterable in more detail I found :
"can not marshal string into timestamp"
... this led me to the answer - GoCQL : Marshal string into timestamp

Rails.cache.read following a Rails.cache.increment does not give correct value

Using Rails 3 with memcachestore and the memcache-client gem.
Local memcache on a macbook-pro or memcache servers on the staging environment.
When I do a
Rails.cache.increment(key, 1)
followed very quickly (w/in a few lines) by a
Rails.cache.read(key, :raw => true)
I get my original value. If it sits for a second and then I call read (or in the console), I get the correctly incremented value.
As my work around, I'm using the returned value from the increment call, but this doesn't seem like it should be happening.
Any ideas?
This problem is described in a blog post
http://thomasmango.com/2009/06/25/a-better-rails-cache-increment/
and the suggested fix is:
class Util::Cache
def self.increment(key, amount = 1)
if (value = Rails.cache.read(key)).nil?
Rails.cache.write(key, (value = amount))
else
Rails.cache.write(key, (value = value + amount))
end
return value
end
def self.decrement(key, amount = 1)
if (value = Rails.cache.read(key)).nil?
value = 0
else
Rails.cache.write(key, (value = value - amount))
end
return value
end
end