按值查找列

时间:2017-10-23 22:31:08

标签: ruby-on-rails rails-activerecord

class Child
  has_many :toys
end


class Toys
  belongs_to :child
end

class CreateToys < ActiveRecord::Migration[5.1]
def change
  create_table :toys do |t|
  t.integer :child_id
  t.boolean :marbles, default: false
  t.boolean :blocks,  default: false
  t.boolean :jacks,   default: false
  t.boolean :dolls,   default: false
  t.timestamps
  end
 end
end

child = Child.first
child.toys.create(marbles=true, blocks=true, jacks=false, dolls=false)

如何执行查询以返回孩子所拥有的玩具? 我不想在查询中使用列名,以便我可以将玩具添加到列表中,并且能够找到任何标记为true的内容,而无需指定每个。

1 个答案:

答案 0 :(得分:-1)

input_list = [102, 97, 116, 101]
sorted_list = sorted(input_list, reverse=True)
result = []
for n in input_list:
    result.append(sorted_list.index(n))

# result now contains [1, 3, 0, 2]