Reputation: 3
I'm trying to get an understanding about the inner features of Active Record playing with IRB.
I have a small database with a few tables which I created using SQL. Then I connected Active Record to that database using a Ruby script. I already loaded the models, which are classes with the tables' names singularized, and made all attributes accessible via attr_accessor
but I only get the id and the rest return nil when trying to get them. Any clue?
I have the table "empresas" with four records and the class Empresa:
2.2.1 :001 > load 'config.db'
=> true
2.2.1 :002 > e = Empresa.all
=> #<ActiveRecord::Relation [#<Empresa id: 1, razon_social: "Holbrook Co.", cuit: "33-22564787-4", domicilio: "San Lorenzo 336", localidad_id: 7>, #<Empresa id: 2, razon_social: "Marshfield Llc.", cuit: "30-13547542-0", domicilio: "Av. de Mayo 110", localidad_id: 2>, #<Empresa id: 3, razon_social: "Iron Cobra Inc.", cuit: "30-24652120-8", domicilio: "9 de Julio 332", localidad_id: 20>, #<Empresa id: 4, razon_social: "Apple Inc.", cuit: "30-43753246-4", domicilio: "Riverside Road 342", localidad_id: 4>]>
2.2.1 :003 > e[2]
=> #<Empresa id: 3, razon_social: "Iron Cobra Inc.", cuit: "30-24652120-8", domicilio: "9 de Julio 332", localidad_id: 20>
2.2.1 :004 > e[2].id
=> 3
2.2.1 :005 > e[2].razon_social
=> nil
2.2.1 :006 > e[2].cuit
=> nil
The model looks like this:
class Empresa < ActiveRecord::Base
belongs_to :localidad
attr_accessor :razon_social, :cuit, :domicilio, :localidad_id
end
This model is required in the script that makes the connection to the database, along with active_record
and fb
(I'm using firebird for the database).
Upvotes: 0
Views: 378
Reputation: 1874
ActiveRecord will automatically create accessors for columns in your database tables.
When you define accessors with attr_accessor
, you override those accessors.
Try removing that call to attr_accessor
, then your calls should go on to the database values!
Upvotes: 2