Check if element in array was created_at within last 5 seconds - ruby-on-rails

I have an array #user.photos which will return something like:
[#<Photo id: 53, photo_file_name: "Freedom-Tower",
photo_content_type: "image/jpeg", photo_file_size: 1937702,
photo_updated_at: "2013-04-23 23:57:22", user_id: 1,
created_at: "2013-04-23 23:57:23", updated_at: "2013-04-23 23:57:23">,
#<Photo id: 52, photo_file_name: "sunset-Courtsey",
photo_content_type: "image/jpeg", photo_file_size: 1937702,
photo_updated_at: "2013-04-23 23:51:22", user_id: 1,
created_at: "2013-04-23 23:51:24", updated_at: "2013-04-23 23:51:24">
I want to create a conditional that will check if a user has recently created a photo
Something like: #user.photos.any? { |x| x.created_at btw Time.now..-5 seconds }??

You can use 5.seconds.ago:
#user.photos.any? {|x| x.created_at > 5.seconds.ago }

How about:
#user.photos.any? { |x| x.created_at > (Time.now-5.seconds) }
Is there a reason for the upper bound? Can photos be created in the future?

Related

How to sort collection?

How to sort collection on parametr n_text?
abc = [108, [#<Advert id: 7095, user_id: 5, n_text: "First text", created_at: "2019-06-03 00:49:00", images_count: 0>, #<Advert id: 7096, user_id: 7, n_text: "Second text", created_at: "2019-06-03 08:23:00", images_count: 1>,...]]
abc.order_by { |k, v| v[:n_text] } dont working.
abc.last.sort_by(&:n_text)
I think this will work, .last because you are going to sort last element of the abc array.

After loop always nil in Rails 4

When I try cycle the rails array I have still nil, even when array already defined.
The code:
def gen_address
current_user.accounts.each do |account|
abort(#account.inspect)
next if not account.currency_obj.coin?
if account.payment_addresses.blank?
account.payment_addresses.create(currency: account.currency)
else
address = account.payment_addresses.last
address.gen_address if address.address.blank?
end
end
render nothing: true
end
current_user.accounts have array with three items. abort is there for testing only.
current_user.accounts have:
#<ActiveRecord::Associations::CollectionProxy [#<Account id: 14, member_id: 3, currency: nil, balance: 0.0, locked: 0.0, created_at: "2017-05-23 08:50:11", updated_at: "2017-05-23 08:50:16", in: nil, out: nil, default_withdraw_fund_source_id: nil>, #<Account id: 5, member_id: 3, currency: "btc", balance: 0.0, locked: 0.0, created_at: "2017-05-03 08:37:19", updated_at: "2017-05-03 08:37:19", in: nil, out: nil, default_withdraw_fund_source_id: nil>, #<Account id: 6, member_id: 3, currency: "ltc", balance: 0.0, locked: 0.0, created_at: "2017-05-03 08:37:19", updated_at: "2017-05-03 08:37:19", in: nil, out: nil, default_withdraw_fund_source_id: nil>, #<Account id: 13, member_id: 3, currency: "eth", balance: 0.0, locked: 0.0, created_at: "2017-05-23 08:42:29", updated_at: "2017-05-23 08:42:35", in: nil, out: nil, default_withdraw_fund_source_id: nil>]>
account is always nil
currency_obj is:
module HashCurrencible
extend ActiveSupport::Concern
included do
def currency_obj
Currency.find_by_code(attributes[:currency])
end
end
end
I get this error for row next if not account.currency_obj.coin?, when remove abort:
undefined method `coin?' for nil:NilClass
currenc_obj is nil, so it can't respond to coin? method; check this line:
#<Account id: 14, member_id: 3, currency: nil, balance: 0.0, locked: 0.0, created_at: "2017-05-23 08:50:11", updated_at: "2017-05-23 08:50:16", in: nil, out: nil, default_withdraw_fund_source_id: nil
In your first Account object, the attribute :currency is nil so the method currency_obj will return nil as well.
The variable account has scope only to the loop. So it will be nil outside the loop.

On rails console output what does it mean #< on the following example?:

On rails console output what does it mean #< at the start of the hash on the following example?:
irb(main):003:0> a=Movie.all
Movie Load (0.5ms) SELECT "movies".* FROM "movies"
=> [#<Movie id: 1, title: "Aladdin", rating: "G", description: nil, release_date: "1992-11-25 00:00:00", created_at: "2013-07-27 21:29:01", updated_at: "2013-07-27 21:29:01">, # <Movie id: 2, title: "The Terminator", rating: "R", description: nil, release_date: "1984-10- 26 00:00:00", created_at: "2013-07-27 21:29:01", updated_at: "2013-07-27 21:29:01">, #<Movie id: 3, title: "When Harry Met Sally", rating: "R", description: nil, release_date: "1989-07-21 00:00:00", created_at: "2013-07-27 21:29:01", updated_at: "2013-07-27 21:29:01">,... more output
That's how an object is printed in ruby, for example and instance of class Movie would be printed something like this:
<#Movie:0x003247fa... >
| |
class memory position I think
What you have there is a set of this previous writing:
[ one_object, other_object ... ]
To have a better display you could use hirb.
#< means that this is an instance of the Movie class.

Rails 4 Partial with Collection

I am doing something obviously wrong here, but I don't get it.
I have a partial call like so:
= render 'shared/purchase', collection: #purchases
#purchases is defined in the controller like so:
#purchases = current_user.purchases
But I get this error:
app/views/shared/_purchase.html.slim where line #3 raised:
undefined local variable or method `purchase' for #<<Class:0x007fd8ca54f970>:0x007fd8ca54eb60>
However, when I change the partial to just render the local_assigns I see this:
[:collection, #<ActiveRecord::Associations::CollectionProxy [#<Purchase id: 40, user_id: 20, purchaseable_id: 6, purchaseable_type: "PurchaseType", frequency: "Weekly", day: "Su", notes: "leave at gate", allergies: "peanuts", created_at: "2013-07-24 16:58:08", updated_at: "2013-07-24 16:58:08", size: "Normal", quantity: 2>, #<Purchase id: 41, user_id: 20, purchaseable_id: 5, purchaseable_type: "PurchaseType", frequency: "Weekly", day: "Su", notes: "leave at gate", allergies: "peanuts", created_at: "2013-07-24 16:58:08", updated_at: "2013-07-24 16:58:08", size: "Jumbo", quantity: 3>, #<Purchase id: 42, user_id: 20, purchaseable_id: 7, purchaseable_type: "PurchaseType", frequency: "Weekly", day: "Su", notes: "leave at gate", allergies: "peanuts", created_at: "2013-07-24 16:58:08", updated_at: "2013-07-24 16:58:08", size: nil, quantity: 1>, #<Purchase id: 43, user_id: 20, purchaseable_id: 8, purchaseable_type: "PurchaseType", frequency: "Weekly", day: "Su", notes: "leave at gate", allergies: "peanuts", created_at: "2013-07-24 16:58:08", updated_at: "2013-07-24 16:58:08", size: nil, quantity: 1>]>]
[:purchase, nil]
Why isn't each collection item getting picked up as the local variable purchase?
I'm tearing my hair out, I'm sure it's something stupid, I've done this many times before, and even compared old working code to this and I can't figure out what the problem is. Either it's a new Rails 4 thing that I'm not seeing in the docs, or I'm an idiot.
Thanks!
The Rails documentation on Layouts and Render in Rails gives an example using the partial parameter for render:
<h1>Products</h1>
<%= render partial: "product", collection: #products %>

Converting an array into string

I need to join output of a hash into a string.
The hash looks like this:
nas.location.walledgardens.to_s
=> "[#<Walledgarden id: 1, location_id: 12, url: \"polka.com\", created_at: \"2012-05-14 17:02:47\", updated_at: \"2012-05-14 17:02:47\">, #<Walledgarden id: 2, location_id: 12, url: \"test.com\", created_at: \"2012-05-14 17:02:47\", updated_at: \"2012-05-14 17:02:47\">, #<Walledgarden id: 3, location_id: 12, url: \"help.com\", created_at: \"2012-05-14 17:02:47\", updated_at: \"2012-05-14 17:02:47\">, #<Walledgarden id: 4, location_id: 12, url: \"yell.com\", created_at: \"2012-05-14 17:02:47\", updated_at: \"2012-05-14 17:02:47\">, #<Walledgarden id: 5, location_id: 12, url: \"sausage.com\", created_at: \"2012-05-14 17:02:47\", updated_at: \"2012-05-14 17:02:47\">]"
I need to join the url values into the following format:
polka.com,test.com,help.com
What the best way to do this? I can easily look through it but the output has line breaks and I need these removed plus the commas.
nas.location.walledgardens.collect { |w| w.url }.join(",")
The .collect method will collect all what that block returns and put it in an array, and then the join puts that array in a string, separated by whatever you give it (so a comma).
What you have is not a Hash, but an Array of Walledgarden objects (they look to be ActiveRecord::Base subclasses).
Try this:
nas.location.walledgardens.collect(&:url).join ","
(Note: #map and #collect are equivalent, so which one you choose should be a consideration of readability!)
Use Array#map:
nas.location.walledgardens.map(&:url).join ','

Resources