Problems with CamelCase and underscore Rails 3 -


i have 2 models has many through association between them below:

tipodocumento < activerecord::base     has_many :dependencias     has_many :tiporequisitos, :through => :dependencias     ... end  tiporequisito < activerecord::base     has_many :dependencias     has_many :tipodocumentos, :through => :dependencias     ... end  dependencia < activerecord::base     belongs_to: tipodocumento     belongs_to: tiporequisito     ... end 

the id's attributes join model dependencia tipodocumento_id , tiporequisito_id.

now, when try in rails console:

x = tipodocumento.find(1) x.tiporequisitos 

i error:

activerecord::statementinvalid: sqlite3::sqlexception: no such column: dependencia.tipo_documento_id: select "tipo_requisitos".* "tipo_requisitos" inner join "dependencia" on "tipo_requisitos"."id" = "dependencia"."tiporequisito_id" "dependencia"."tipo_documento_id" = 1 

also if try opposite tiporequisito it's same.

it seems rails changing somehow tipodocumento_id column name tipo_documento_id when performs query. so, tried change id's column's names camelcase snake_case, analog error (cannot find tipodocumento_id or tiporequisito_id.)

i don't see what's wrong.

you need follow rails convention , use down-cased names when refer models when defining relations:

 tipodocumento < activerecord::base      has_many :dependencias      has_many :tipo_requisitos, :through => :dependencias      ...  end   tiporequisito < activerecord::base      has_many :dependencias      has_many :tipo_documentos, :through => :dependencias      ...  end  dependencia < activerecord::base     belongs_to: tipo_documento     belongs_to: tipo_requisito     ...  end 

you need lower-case it, this:

  x = tipodocumento.find(1)   x.tipo_requisitos 

please check: http://guides.rubyonrails.org/association_basics.html


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -