Tengo el siguiente problema:
El esquema de Mi base de datos se ve así:
ActiveRecord::Schema.define(version: 20180115094430) do
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.string "remember_digest"
t.boolean "admin"
t.string "activation_digest"
t.boolean "activated"
t.datetime "activated_at"
t.string "reset_digest"
t.datetime "reset_sent_at"
t.float "zero_till_one"
t.float "one_till_two"
t.float "two_till_three"
t.float "three_till_four"
t.float "four_till_five"
t.float "five_till_six"
t.float "six_till_seven"
t.float "seven_till_eight"
t.float "eight_till_nine"
t.float "nine_till_ten"
t.float "ten_till_eleven"
t.float "eleven_till_twelve"
t.float "twelve_till_thirteen"
t.float "thirteen_till_fourteen"
t.float "fourteen_till_fifteen"
t.float "fifteen_till_sixteen"
t.float "sixteen_till_seventeen"
t.float "seventeen_till_eightteen"
t.float "eightteen_till_nineteen"
t.float "nineteen_till_twenty"
t.float "twenty_till_twentyone"
t.float "twentyone_till_twentytwo"
t.float "twentytwo_till_twentythree"
t.float "twentythree_till_zero"
end
end
Cuando trabajo con la base de datos, quiero acceder a los atributos / columnas zero_till one
hasta twentythree_till_zero
y asignarlos junto con sus valores a un hash. Sé que puedo acceder a todos los valores con @user.zero_till_one
y así sucesivamente, pero no existe una forma de indexar las columnas y recorrer los índices para guardarlos en un hash o algo similar.
Perdón por preguntar que soy muy nuevo en RoR y la aplicación que estoy programando es la primera aplicación después del tutorial de Michael Hartl.
Cualquier ayuda o sugerencia es muy apreciada
3 respuestas
Si no lo entiendo mal, entonces quieres hash del objeto de usuario. Puede usar user.serializable_hash
para convertir el objeto de usuario en hash o explicar más detalladamente sus requisitos.
Además, puede usar el método attributes
del registro activo.
user.attributes.except('name', 'email')
Creo que este es un enfoque más limpio.
user.serializable_hash.except('name','email')
Nuevas preguntas
ruby-on-rails
Ruby on Rails es un marco de aplicación web full-stack de código abierto escrito en Ruby. Sigue el popular modelo de marco MVC y es conocido por su enfoque de "convención sobre configuración" para el desarrollo de aplicaciones.