AlexCs
AlexCs

Reputation: 67

Create associations in RoR

In my form I have a select of a type_control_access in which I have three options, which the user chooses an option I want to save in a column in a table called control_accesses.

my model type_control_access:

class TypeControlAccess < ActiveRecord::Base

  has_many :control_accesses

  WORKSHOPS = 1
  CONFERENCES = 2
  PAPERS = 3

end

my model control_access:

class ControlAccess < ActiveRecord::Base

  belongs_to :type_control_access

end

but I already have an existing table:

class CreateControlAccesses < ActiveRecord::Migration
  def change
    create_table :control_accesses do |t|
      t.string :name
      t.string :description

      t.timestamps null: false
    end
  end
end

I want to save the id of the type_control_access in type_control_access_id my control_access but with the rails associations, how can I do this?

I want something like this:

name: "antenna1", description: "this antenna is installed on the door", type_control_access_id: 2

Upvotes: 0

Views: 60

Answers (1)

Nezir
Nezir

Reputation: 6905

As I can understand your request, you should create new migration to add new reference field between tables:

Sample in your terminal:

rails g migration AddTypeControlAccessToCreateControlAccesses TypeControlAccess:references

after that rub rails db:migrate and that would work.

Upvotes: 1

Related Questions