if i give
p #work_orders.where(wo_number: 34305)
in my controller, o/p in terminal will be
#<ActiveRecord::Relation [#<WorkOrder id: 9168, shop_number: 3, ticket_number: 0, wo_number: 34305, date: "2014-04-02 02:24:57", time: "2000-01-01 15:54:57", customerPO: "", warranty_code: "1", market_source: "", sub_total: #<BigDecimal:bba2164,'0.72456E3',18(18)>, discount_type: "dollar", discount_amount: #<BigDecimal:bba20b0,'0.0',9(18)>, tax: #<BigDecimal:bba2038,'0.39E0',9(18)>, tax_exemptID: "", order_type: "regular", created_at: "2014-07-17 06:25:26", updated_at: "2014-07-17 06:25:26">]>
i'm trying to retrieve the value of subtotal..
This is so far i have tried..
p #work_orders.where(wo_number: 34305).first.sub_total
that resulted in error
undefined method `sub_total' for nil:NilClass
Related
When I run ActiveRecord queries, the Rails Console seems to be appending LIMIT 1 to my queries.
So I have a sheet which has_many slots. When I query Slot.find_by(sheet_id: 96), I get:
Slot Load (2.3ms) SELECT "slots".* FROM "slots" WHERE "slots"."sheet_id" = ? LIMIT 1 [["sheet_id", 96]]
=> #<Slot id: 153, label: "Foo", name: "Foo", email: "", phone: "", comments: "Fighters", sheet_id: 96, created_at: "2015-04-30 14:28:47", updated_at: "2015-04-30 14:28:47">
But when I query Sheet.find(96).slots:
Sheet Load (10.0ms) SELECT "sheets".* FROM "sheets" WHERE "sheets"."id" = ? LIMIT 1 [["id", 96]]
Slot Load (4.6ms) SELECT "slots".* FROM "slots" WHERE "slots"."sheet_id" = ? [["sheet_id", 96]]
=> #<ActiveRecord::Associations::CollectionProxy [#<Slot id: 153, label: "Foo", name: "Foo", email: "", phone: "", comments: "Fighters", sheet_id: 96, created_at: "2015-04-30 14:28:47", updated_at: "2015-04-30 14:28:47">, #<Slot id: 154, label: "Bar", name: "James", email: "", phone: "", comments: "Foobar", sheet_id: 96, created_at: "2015-04-30 14:28:47", updated_at: "2015-04-30 14:28:47">, ... >
You have to do Slot.find_all_by_sheet_id(96)
EDIT The above code should have worked. Although I use Rails 4.1.8. Try following as well:
Slot.where(:sheet_id => 338)
The find_by method returns a single result, always.
If you want to get all of the slots for a particular sheet, there are a few options:
Sheet.find(96).slots or more likely #sheet.slots if you've already found the sheet
Slot.where(sheet_id: 96) would also work
To be clear, this has nothing to do with the Rails console and everything to do with the .find_by method.
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
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.
I have such array data:
[#<PriceList id: 463134, distributor_id: 6, brand: "Mann-filter", article_nr: "W712/22", price: 5405.0, quantity: "50", waittime: 1, description: "Фильтр масл OPEL 1.2-3.0L (OC90)", created_at: "2013-01-30 16:35:34", updated_at: "2013-01-30 16:35:34", art_group: "Фильтр масл OPEL 1.2-3.0L (OC90)", oem_number: nil>, #<PriceList id: 517164, distributor_id: 6, brand: "Mann-filter", article_nr: "W712/22", price: 5442.0, quantity: "500", waittime: 3, description: "Фильтр масляный OPEL/GM/DAEWOO", created_at: "2013-01-30 16:42:26", updated_at: "2013-01-30 16:42:26", art_group: "Фильтр масляный OPEL/GM/DAEWOO", oem_number: nil>, #<PriceList id: 463135, distributor_id: 6, brand: "Mann-filter", article_nr: "W712/22(10)", price: 5101.0, quantity: "20", waittime: 1, description: "Фильтр масл.без упак.OPEL/GM (OC90Of)", created_at: "2013-01-30 16:35:34", updated_at: "2013-01-30 16:35:34", art_group: "Фильтр масл.без упак.OPEL/GM (OC90Of)", oem_number: nil>, ... etc
how can i change price type?
i try
#non_original2 = #non_original2.map { |e| e[:price].to_i }
but as result i see only price values... How can i change my array, so that price field in all hashes become integer value?
what about
#non_original2.each { |e| e[:price] = e[:price].to_i }
This changes every PriceList item in the list (and does not copy the list).
Using your approach results in a list of price values, because map collects the result of the block. The result of e[:price].to_i is an integer (the prices you see).
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 %>