Accessing Rails Link Table Fields

1

MODIFIED QUESTION:

I have an application that has 2 tables: Curso and Cargo and a third table curso_cargo that makes the HBTM between them. I would like to know how to access a field of this curso_cargo table, to include data in it by a text_field ?

I have an application that has 3 tables: Conhecimentos , Cargo and cursos_cargos .

create_table :cargos do |t|
  t.integer :id
  t.string :nome
  t.text :missao
  t.text :atribuicoes
  t.text :formacao
  t.text :experiencia
  t.references :setor
  t.timestamps
end

create_table :conhecimentos do |t|
      t.string :nome
      t.timestamps
    end

create_table :conhecimentos_responsabilidades, :id => false, :force => true do |t|
      t.references :cargo
      t.references :conhecimento
      t.references :nivel
    end

In this last table, I have a level_id field, which will store a one-level id for each knowledge registered for that job. This field will be in view of Positions, which will display what the existing knowledge and level of knowledge.

How do I create form , indicating that Cargo , has a conhecimento with nível x?

    
asked by anonymous 20.06.2014 / 15:10

1 answer

1

You are creating the membership table with% no need and almost form erroneous

Rails works unanimously, where the convention is on the configuration - and this only makes it necessary to use two tables: curso_cargo and curso . If you need to inject non-associative information into your third table ( cargo ), regardless of Rails or not, you are wrong - join tables serve only and only to associate entities (X) with entities (Y).

According to the nomenclature of your membership table, curso_cargo , I assume curso_cargo belongs to cargo , with one or more courses being able to share the same job. So in curso , you teach to /app/assets/models/curso.rb that he has a charge:

class Curso < ActiveRecord::Base
    has_many :cargo
end

And in curso , you explain that he [the position] belongs to a course:

class Cargo < ActiveRecord::Base
    belongs_to :curso
end

Once this is done, Rails will take care of the association for you. Besides this practicality, you are bringing up a great - and standard - practice.

Highlighting, your /app/assets/models/cargo.rb table will need a curso as follows:

+----+------+----------+
| id | nome | cargo_id |
+----+------+----------+

To manipulate this with cargo_id , you can do:

[...]

<%= f.text_field :curso, :cargo_id %>

[...]
    
20.06.2014 / 16:44