ActiveRecord limit(7) - should always pull 7 - ruby-on-rails

I have the following rails query:
#related_products = #taxon.products.offset(rand(Spree::Product.count)).limit(7)
And sometime it outputs 7 and sometimes it will output less, but when it outputs less it messes up the styling. Anyway to make it always output 7 and only 7?
Thanks!

That's because the offset that you are choosing has the possibility of being less than 7 away from the last record. Instead force the offset to max out at 7 below the maximum:
#related_products = #taxon.products.offset(rand(Spree::Product.count - 7)).limit(7)

Related

Converting some values in a variable from minutes to hours while keeping the rest the same

In a questionnaire I asked people for how long they ususally sleep a night. Now replys were supposed to be in a h.mm format but were accidently set to h only. This is why some participants gave their sleep duration in minutes or simply wrote "830" for 8 hours 30 minutes. Now I wanted to correct for both variations and tried this first:
RECODE Sleep (1=1) (2=2) (3=3) (4=4) (5=5) (6=6) (7=7) (8=8) (9=9) (10=10)(11=11)(12=12)(13=13)(14=14)(801 thru 899 = 8.5) (701 thru 799 = 7.5)(200 thru 600 = (Sleep/60))
INTO Sleep_rek.
RECODE Sleep_rek(LOWEST thru 4.5 = 0) (5, 6= 1) (6.5 thru 7 = 2) (7.5 thru HIGHEST=3)
INTO PSQI_K3_SleepDuration.
Execute.
Since it didn't work and I thought maybe Recode can't do that, I tried this instead:
RECODE Sleep (1=1) (2=2) (3=3) (4=4) (5=5) (6=6) (7=7) (8=8) (9=9) (10=10)(11=11)(12=12)(13=13)(14=14)(801 thru 899 = 8.5) (701 thru 799 = 7.5)
INTO Sleep_rek.
IF (Sleep = (300 thru 600) & Sleep_rek = sysmis) Sleep_rek = (Sleep/60).
RECODE Sleep_rek(LOWEST thru 4.5 = 0) (5, 6= 1) (6.5 thru 7 = 2) (7.5 thru HIGHEST=3)
INTO PSQI_K3_SleepDuration.
Execute.
However this threw an error as well:
The code is incomplete, check for missing operants, invalid operants,
non-matching paranthesis or too long strings.
The PSQI_K3_SleepDuration variable in the end was computed but anyone with a 300 to 600 value is still a missing value.
Can anyone tell me how to put it so it will work?
I can see a few errors in your syntax - see if it works once you've corrected them.
Indeed, recode .... (200 thru 600 = (Sleep/60)) can not work, you can't use a calculation as the target value.
recode ... (5, 6= 1) should be (5 6= 1) instead (no comma needed).
IF (Sleep = (300 thru 600) & Sleep_rek = sysmis) Sleep_rek = (Sleep/60) has a couple of errors: thru ia a subcommand for recode, can't be used like this, also can't use "sysmis" this way. Corrected version:
IF (Sleep>= 300 and Sleep<=600) and missing(Sleep_rek) Sleep_rek = (Sleep/60).
Your last recode is fine syntax-wise, but the ranges you are using seem to be wrong - since you'll be dividing numbers from 300 to 600 in 60, you will only get fractions in values 5 to 10. Yet the recode covers fractions in numbers under 4.5 and doesn't cover them in values between 5 to 7.5 (except specific values: 5, 6, 6.5, 7).

How to break from a loop in Maxima

I am new to Maxima. I am trying to write a loop in that I am checking if some condition met then exit from the loop.
cp:for i:1 step 1 thru 10 do
block(if(i>6) then break()
else
print(i,"is less than 6"));
I want output:
1 is less than 6
2 is less than 6
3 is less than 6
4 is less than 6
5 is less than 6
6 is less than 6
But when I am running the above code :
after printing 6 is less than 6, it is prompting
Entering a Maxima break point. Type 'exit;' to resume.
and after typing exit; it will again show the above msg
I want the code will come out completely from that loop rather than asking to type exit;
Thank you in advance..
Try return(i) instead of break(). Also, return only returns from the block which encloses it, so you need to remove the block(...) in your example (it's unneeded anyway). I think this works:
cp: for i:1 step 1 thru 10
do if(i>6) then return(i) else print(i,"is less than 6");

Generated integer array based on number in rails

I want to generate 5 buttons with different values based on one integer.
For example I've got 30, I want to create buttons with 10 20 30 40 50
value = 30
int1 = value - 20
int2 = value - 10
int3 = value
int4 = value + 10
int5 = value + 20
buttoncode = ""
%w{int1 int2 int3 int4 int5}.each do |minutes|
buttoncode += 'buttoncode'
end
I can do it in a very bad way, but it could be done a smarter solution I guess.
Is it possible to make something like that?
%w{sum(max-20) sum(max-10) max sum(max+10) sum(max+20)}.each do |minutes|
end
See Ruby: How to iterate over a range, but in set increments?
So in your case it would be:
(min..max).step(10) do |n|
n += 'buttoncode'
end
By the way, this is not really Rails specific, but Ruby specific. Rails is a web framework that handles the interaction between browser and the web server that is built on top of Ruby.
If you feel like you aren't that up to speed with Ruby, try https://learnrubythehardway.org/book/ and do some exercise on HackerRank or ProjectEuler in Ruby.

websql ios7 how to increase size?

as described here: http://www.mobilexweb.com/blog/safari-ios7-html5-problems-apis-review
I want to take advantage of the workaround described.
I tried:
var size = 4, db = openDatabase("db", "v1.1", "db", size * 1024 * 1024);
then I tried:
var size = 40, db = openDatabase("db", "v1.1", "db", size * 1024 * 1024);
And this, I've tried over thousand times the last 24 hours.
Sometimes a prompt appears: "increase up to 5MB?" .. but never one with "increase up to 10 or 25 or 50 …………"
Some time at the beginning of this, it worked! … Crazy. It really worked. But I wasn't sure about how I came to this point .. that's why I started to do it again (and again …) .. but it never worked again :( :(
… 23 hours later …
I have to show it to the client tomorrow, but it doesn't work. damn! That is why you are my last hope!
So If anybody has experience with websql-cache-limit on ios7 … please tell me what I'm doing wrong …
Thx!
I am editing the answer, as i didn't noticed you already saw that link. but, the same work around works for me. I am initially asking for 1 MB of space and then it works properly.
var db=openDatabase('PermissionDatabase', '1.0', 'PermissionDB', 1 * 1024 * 1024);
As per your answer, it worked initially, try resetting safari to delete all the previous permissions granted and then try again.
Initial request database size has no meaning in iOS.
Workaround is, dump garbage data of your desire storage size in temp db. This should cause to request quota. Then you delete temp db, and create your db.

How do I convert a floating point number into erlang time format (and vice versa)?

I am trying to convert an erlang time format tuple, {megasec,sec,microsec}, into a floating point number and back again.
I can do this one way, e.g.:
{Megasec,Sec,Usec} = erlang:now().
Total = Megasec*1000000+Sec+Usec/1000000.
1352802601.427
But I am struggling to convert this number back to the time format. I have a general idea to divide by 1000000 and round but I get rounding errors. e.g.
Mega = erlang:round(Total/1000000).
1353
If I could get this accurately I could apply similar steps to get Seconds and Microseconds.
Any ideas?
You can use erlang:trunc instead of erlang:round.
Following #Falco Hirschenberger's suggestion here's how I did it:
Mega = erlang:trunc(Total/1000000).
1352
Sec = erlang:trunc(Total - Mega*1000000).
802601
Usec = erlang:round((Total - Mega*1000000 - Sec)*1000000).
427000
Note. I had to use erlang:round to get Usec (else the answer would have been 427000.0457763672 - I think this is due to a rounding error introduced when I divided by 1000000)

Resources