Rails unexpectedly creating duplicated records - ruby-on-rails-4

I need to create a new market "3.5" when the "0.5" closed. The problem is, for some reason, many times, and only on the live server it created duplicaded "3.5", exactly at the same time when it closed the 0.5 and opens the first 3.5. Is there any way to prevent duplicated records from being created? The majority of times it creates single records, but for some reason I can't figure out, sometimes it messes up.
Here is part of the problematic code:
######CLOSE 0.5 / OPENS 3.5
if self.markets.find_by_name('0.5') != nil then
if result.sum >= 1 && (self.markets.find_by_name('0.5').status == "live" || self.markets.find_by_name('0.5').status == "pre-live")
if self.markets.find_by_name('0.5').settle_temp.to_i == 6
selection = "Over 0.5 Goals"
if self.markets.find_by_name('0.5').status == "live" || self.markets.find_by_name('0.5').status == "pre-live"
self.markets.find_by_name('0.5').close(selection)
end
if self.markets.find_by_name('3.5') == nil then
self.markets.create!(name: "3.5", status: "live")
end
else
self.markets.find_by_name('0.5').increment!(:settle_temp)
end
end
end

Don't know your exact situation, but maybe you could add unique constraint
on name and event_id fields pair? For example in the migration you could write:
add_index :markets, [:event_id, :name], :unique => true
In this way you'll be sure that there is just one markets record with desired name value.

Related

Equation in if branch is not executed

I have a question that confused me for a long time. As you know, when we use an if condition in Modelica, that means if the expression is true, then Modelica will do the corresponding equation.
But when i test the following code, I am confused:
model Model134
Real a(start = 0);
equation
if not sample(0, 2) then
a = 1;
else
a = 3;
end if;
end Model134;
I think a will be changed every 2s (start time=0), but when I simulate this model, it dose not change and a is equal to 1 all the time.
Dose anybody know the root cause?
a does change its value, but depending on your simulation tool you might not see it in the plot.
sample(0, 2) creates a time event every 2 seconds. The return value of sample() is only true during the event. So the value of a changes, but after the event it immediately changes back.
In this answer to a similar question, it is mentioned that Dymola stores the value before and after the event in result file. Intermediate values are skipped for efficiency reasons (there can be many for every event, which would bloat up your result file). Hence you can not plot this change in Dymola. For OpenModelica see the answer by
Akhil Nandan.
To proof that a really does change its value you can use this code for example:
model Model134
import Modelica.Utilities.Streams.print;
Real a;
equation
if sample(0, 2) then
a = 1;
else
a = 0;
end if;
when a > 0.5 then
print("a is " + String(a) + " at t=" + String(time) + "s");
end when;
annotation (experiment(StopTime=10));
end Model134;
You should see something like this in the simulation log:
a is 1 at t=2s
a is 1 at t=4s
a is 1 at t=6s
a is 1 at t=8s
a is 1 at t=10s
This is the plot simulated when trying your above code in OpenModelica with settings shown in the second figure.
A time event is triggered when sample(startTime,interval) evaluates true at every multiple of 2 seconds and based on your code logic this should activate else
block and assign value of variable a to be 3.

Python 3.7 : Why is only the last item in a list printing?

I am self-studying "Python Crash Course" 2nd edition and I have come upon a problem that I am unable to solve. I have reread previous areas of the book, searched the internet and search other SO answers.
The official book answer uses a dictionary but I am trying to use a list.
The program should ask for input, add the input to a list and continue to repeat until told to stop. When told to stop, the entire list should be printed.
The problem that I am running into is that only the last item in the list in printing.
Instead of giving the answer, please just give me a hint so that I can truly learn.
Thanks,
chadrick
active = True
while active == True:
places = []
place = input("\nIf you could visit one place in the world, where would you go? ")
places.append(place)
repeat = input("Would you like to let another person respond? (yes/no) ")
if repeat == 'no':
print(places)
active = False
The reason is because you're resetting the list back to empty for every iteration.
try the following:
active = True
places = []
while active == True:
place = input("\nIf you could visit one place in the world, where would you go? ")
places.append(place)
repeat = input("Would you like to let another person respond? (yes/no) ")
if repeat == 'no':
print(places)
active = False
Also, since active is a boolean, you just need to do while active:
So:
active = True
places = []
while active:
place = input("\nIf you could visit one place in the world, where would you go? ")
places.append(place)
repeat = input("Would you like to let another person respond? (yes/no) ")
if repeat == 'no':
print(places)
active = False

Power BI If statement

I have created four calculated columns that give True or False for a unique property ref in each row, based on the presence of a registered contact (CMS) on a list (Tele Open, Tele Closed, Written Open, Written Closed). I now need to assign a status to each property ref based on the combination of True False.
I am able to do this using a multiple nested IF AND statement in Excel but am confused about to do this in Power BI.
I haven't yet attempted to do this and need some advice on how to frame the statement, as I am not familiar enough with M Language.
'''
If -- ALL are FALSE = Not Yet Investigated
If -- Tele Open is TRUE = In Progress
If -- Tele Open is FALSE AND Tele Closed is FALSE AND Written Open AND/OR Written Closed is True = In Progress
If -- Tele Open is FALSE AND Tele Closed is TRUE = Closed
'''
The various combination of True False in the categories listed above will produce a Not Yet Investigated, In Progress or Closed status as above.
The basic syntax for a calculated column with an IF statement is
column = if [something] > 1 then [something] else [something else]
you can then nest statement using and and or
column = if [something] >= 1 and [something] <= 10
then "Low"
else if [something] >= 10 and [something] <= 20
then "medium"
else "high"
Hope that helps

How can I create a checkbox filter on c++

I'm having a problem that seems way more easy than it is. I think its more of an algorithmic problem than a coding one.
EDIT:
Imagine you have a database with a name and N boolean parameters, like if the person is blonde or not, if the person likes baseball or not, if person have a smartphone or not etc...
How could you print the name of someone that likes baseball AND is blonde, but doesn't matter if any of the other N parameters are true of false? How can I do that without having to write a test for every single of the (N^2)-1 possibilities?
I created a dictionary that maps a string to a struct with 4 boolean variables and some strings.
I want the user to select which booleans are important to them and return only the information that is true for all variable that he chose.
Something like a checkbox which you can use to filter a column in excel.
For instance if a user chose variables 1 and 2, I would like to know if there is a better way to return the result rather than testing every one of the 16 possibilities, like:
void filter(map<string, Mystruct> Mydictionary, bool bool1, bool bool2, bool bool3, bool bool4){
if(bool1 == true && bool2 == true && bool3 == false && bool4==false){
cout << Mydictionary.bool1Info << Mydictionary.bool2Info
if(bool1 == true && bool2 == false && bool3 == false && bool4==false)
...
Even more, for me its only important to test the booleans that the user picked up, so even if he didn't choose boolean3, it's not important to test if its true or false.
Any ideas?
I would be very glad if anyone could help me with this one
I can help you with your algorithm part ,
instead of checking all 16 possibilities just check 4 conditions separately for bool1,2,3,4 and print their info if they are true.
This way you can complete your task with only 4 if statements.
Hope this answers your query.

IF Formula returning error or not doing a section of the formula

I have a formula in my report to select a field based on requirements:
if not isnull({EXT_TBL.EXT_KEY_TYPE}) then
(if {EXT_TBL.EXT_KEY_TYPE} = "SO" and {EXT_TBL.EXT_ACTION_FLAG_9} = "Y"
then {EXT_TBL.EXT_TEXT})
else '0'
When I run the report it works ok until I try to load a specific page. When I try to load the page I get an error of 'The string is non numeric'. The formula is called in another formula:
{COR_TBL.COR_EXPECTED_DATE} + 2 + ToNumber({#FRM_NOTES})
I have ran the query on the server of:
SELECT * FROM EXT_TBL WHERE EXT_KEY_TYPE = "SO" AND EXT_ACTION_FLAG_9 = "Y";
This returned me two rows of data. I have narrowed it down to a specific entry that is causing the issue, but in the database the row has N in the field action flag 9 instead of Y so it shouldn't be throwing me the error in my report.
The action field 9 is flagged on only two records both of which contain a 7 in the EXT_TEXT feild so I have no idea why I am getting the error.
I also tried a nested if statement of:
if not isnull({EXT_TBL.EXT_KEY_TYPE}) then
(if {EXT_TBL.EXT_KEY_TYPE} = "SO" then (if {EXT_TBL.EXT_ACTION_FLAG_9} = "Y"
then {EXT_TBL.EXT_TEXT}))
else '0'
But it still gave me the same error.
Thanks
I was able to fix the issue by removing the nested if statement and just putting all the conditions in the original statement:
if not isnull({EXT_TBL.EXT_KEY_TYPE}) AND {EXT_TBL.EXT_KEY_TYPE} = "SO"
AND {EXT_TBL.EXT_ACTION_FLAG_9} = "Y"
THEN {EXT_TBL.EXT_TXT} ELSE '0'
This seems to have fixed the issue.