Ask A Question

Notifications

You’re not receiving notifications from this thread.

find_by not returning array but class insance

jason Mardell asked in Databases

What am I missing please?

class Choice < ApplicationRecord
belongs_to :choiceable, polymorphic: true
belongs_to :room_item, optional: false
end

create_table "choices", force: :cascade do |t|
t.string "choiceable_type"
t.integer "choiceable_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "room_item_id"
t.index ["choiceable_type", "choiceable_id"], name: "index_choices_on_choiceable_type_and_choiceable_id", using: :btree
t.index ["room_item_id"], name: "index_choices_on_room_item_id", using: :btree
end

2 records in the database

[#<Choice:0x00007fd3125859e8 id: 32, choiceable_type: "Appliance", choiceable_id: 1, created_at: Tue, 09 Apr 2019 08:42:38 UTC +00:00, updated_at: Tue, 09 Apr 2019 08:42:38 UTC +00:00, room_item_id: 25>,

#]

I can see in the server

Choice Load (0.8ms) SELECT "choices".* FROM "choices" WHERE "choices"."choiceable_id" IN (1, 6) LIMIT $1 [["LIMIT", 1]]

But when I run a query to get them - I don't get a record set as I expect - I get a SINGLE Choice object???????? WTF is going on?

removed = Choice.find_by(choiceable_id: choiceIds)

puts removed.each { | c | c.marked_for_destruction }

**undefined method `each' for #<Choice:0x00007f7f86e09ea0>**
Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,400+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.