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.
Related
I am trying to form an array from the result I got from querying the database
industries = Industry.find_by_name(j['Categories']).industries
The result that i get is as following
[#<Industry id: 3717, staffroom_type: "Industry", name: "Home Service", created_at: "2016-01-19 02:33:30", updated_at: "2016-01-19 05:25:53", parent_id: nil, user_cannot_join_shortlists: false, location: "SYDNEY, NSW, 2000", latitude: -33.8674769, longitude: 151.2069776, shortlist_introduction_email_subject: nil, shortlist_introduction_email_body: nil, external_job_url_enabled: false, deleted: false, deleted_at: nil, account_id: 3506, create_group_permission: false, create_role_permission: false, edit_group_permission: true, uuid: "70ef9351-f517-40a9-a300-8c1078da033a", staffroom_image_file_name: nil, staffroom_image_content_type: nil, staffroom_image_file_size: nil, staffroom_image_updated_at: nil, staffroom_image_repository: "production", cached_staffroom_image_id: nil, industry_type_id: nil, company_id: nil, billed_to: "employer", admin_user_id: nil, send_job_notifications: true, do_not_feature_jobs: false, company_type: nil, company_type_other: nil, company_restriction: nil, shortlist_count: nil>,
#<Industry id: 1624, staffroom_type: "Industry", name: "Aged and Disability Care", created_at: "2015-04-13 02:07:53", updated_at: "2017-05-30 10:49:17", parent_id: nil, user_cannot_join_shortlists: false, location: "NOT PROVIDED (Assuming Sydney, 2000)", latitude: nil, longitude: nil, shortlist_introduction_email_subject: nil, shortlist_introduction_email_body: nil, external_job_url_enabled: false, deleted: false, deleted_at: nil, account_id: 3506, create_group_permission: false, create_role_permission: false, edit_group_permission: true, uuid: "f4b7bac3-3587-4056-a88f-9a9e98c42197", staffroom_image_file_name: nil, staffroom_image_content_type: nil, staffroom_image_file_size: nil, staffroom_image_updated_at: nil, staffroom_image_repository: "production", cached_staffroom_image_id: nil, industry_type_id: nil, company_id: nil, billed_to: "employer", admin_user_id: nil, send_job_notifications: true, do_not_feature_jobs: false, company_type: nil, company_type_other: nil, company_restriction: nil, shortlist_count: nil>]
I would like to convert this result into an array that just looks like this
['Home Service', 'Aged and Disability Care']
These are the name of industries that i get from database.
This is what i tried
industries.each do |industry|
ind[] = industry['name']
end
but I get an error
NoMethodError: undefined method `each' for #
What am I doing wrong here?
This is what j['Categories'] looks like
Disability Support Worker: Disability Support Worker
Use the built-in pluck method:
Industry.where(name: j['Categories']).pluck(:name)
This will be much more performant than map: The map solution will retrieve every column and instantiate an Industry object for each record, then call the name method on each object. pluck will retrieve only the given column and return an array of strings.
I have a collection of user.paid_subscriptions in which each subscription has attributes created_at(datetime) and active(boolean).
How can I check if a PaidSubscription exists such that created_at is not equal to a certain date and active is true?
PaidSubscription looks like this:
[
#<PaidSubscription id: 11457,
user_id: 12,
period: 3,
price: 4000,
expires_at: "2016-03-08 09:44:56",
expires_at: "2016-03-08 09:44:56",
created_at: "2015-12-08 09:44:56",
updated_at: "2016-03-08 23:00:09",
active: false,
giver_id: 20573,
partial: false,
remaining_days: 0>,
#<PaidSubscription id: 13948,
user_id: 12,
period: 1,
price: 1500,
expires_at: "2016-04-11 12:07:40",
created_at: "2016-03-11 13:07:40",
updated_at: "2016-04-11 22:00:11",
active: false,
giver_id: nil,
partial: false,
remaining_days: 0>,
#<PaidSubscription id: 11458....
]
Try this,
If it is a query then it should be like this
user.paid_subscriptions.where("created_at < :date or created_at > :date and active = :active",{date: DateTime.civil(yyyy,mm,dd), active: true})
Or
if it is an array you can use it like below.
user.paid_subscriptions.any? {|ps| ps.active && ( ps.created_at.to_date < Date.civil(yyyy, mm, dd) || ps.created_at.to_date > Date.civil(yyyy, mm, dd))}
When I call:
preivous_lessons = #item.where("track_id = ?", lesson.track_id)
I get this active record realtion:
[#<CodeLesson id: 2, name: "Python", permalink: "python", lesson_content: "", instructions: "Print your name to the console.", hints: "", starting_code: "\"\"\"\r\nThis is a comment\r\n\"\"\"\r\n\r\nprint(\"Hello, World\"...", language_id: "12", order: 1, track_id: 2, user_id: 1, created_at: "2014-02-14 16:01:12", updated_at: "2014-02-15 21:14:43", visible: true>, #<CodeLesson id: 8, name: "Test Lesson", permalink: "test-lesson", lesson_content: nil, instructions: nil, hints: nil, starting_code: nil, language_id: "26", order: nil, track_id: 2, user_id: 1, created_at: "2014-02-20 19:23:15", updated_at: "2014-02-20 19:23:15", visible: false>]
How do I convert this into a usable array of models so I can do something like this:
preivous_lessons.each do |i|
highest = i.order if i.order > highest
end
As OP confirmed from my comment, that my hint solved his problem, I am putting it as an answer to the post :
preivous_lessons = #item.where("track_id = ?", lesson.track_id)
highest = preivous_lessons.maximum(:order)
Documentation of maximum :
Calculates the maximum value on a given column. The value is returned with the same data type of the column, or nil if there's no row.
preivous_lessons = #item.where("track_id = ?", lesson.track_id).all
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 %>
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?