How to initialize has_many attributes in Rails automatically

0

Currently I have 3 models

class Regiao < ActiveRecord::Base
  has_many :tipofretes
  has_many :valorfretes, through: :tipofretes
end

class Tipofrete < ActiveRecord::Base
  has_many :regiao
  has_many :valorfretes, through: :regiaofretes
end

class Valorfrete < ActiveRecord::Base
  belongs_to :regiao
  belongs_to :tipofrete
end

Model TypeFrets there are 5 records that will initially default to the system.

My question is, when I create / save a Region, it automatically loads the records of the TypeFrets and save in the database.

    
asked by anonymous 18.06.2014 / 16:35

2 answers

1

Just to leave it to those who will be having the same problem as mine.

First I did the wrong mapping. The correct one is;

class Regiaofrete < ActiveRecord::Base
  has_many :valorfretes
  has_many :tipofretes, through: :valorfretes

  validates :descricao, presence:  true
end


class Tipofrete < ActiveRecord::Base
  has_many :valorfretes
  has_many :regiaofretes, through: :valorfretes
end

class Valorfrete < ActiveRecord::Base
  belongs_to :regiaofrete
  belongs_to :tipofrete
end

I've inverted the has_many

To start the registry, it must be at the Controller level

def new
    @regiaofrete = Regiaofrete.new
    @regiaofrete.tipofretes << Tipofrete.all
end

The < operator adds objects to a list or array >

    
23.06.2014 / 17:03
1

Rails has standardization to popularize your database, this standardization serves to maintain the maintenance of your app. I do not know how the columns in your database are, so I'll post a generic template:

Within the folder of your Rails project, you will have the following file: db/seed.rb , this is the file responsible for popularizing your database.

You can enter data as follows:

# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
# Examples:
#
#   cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
#   Mayor.create(name: 'Emanuel', city: cities.first)

##
    Departament.create(name: 'Poltronas', subtitle:'', slug: 'poltronas', description: '')
    Departament.create(name: 'Puffs', subtitle:'', slug: 'puffs', description: '')
    Departament.create(name: 'Sofás', subtitle:'', slug: 'sofas', description: '')
    Departament.create(name: 'Chaises', subtitle:'', slug: 'chaises', description: '')
    Departament.create(name: 'Banquetas', subtitle:'', slug: 'banquetas', description: '')
    Departament.create(name: 'Office', subtitle:'', slug: 'office', description: '')
    Departament.create(name: 'Cadeiras', subtitle:'', slug: 'cadeiras', description: '')
    Departament.create(name: 'Mesas', subtitle:'', slug: 'mesas', description: '')
    Departament.create(name: 'Itens', subtitle:'', slug: 'itens', description: '')
    Departament.create(name: 'Pronta Entrega', subtitle:'', slug: 'pronta-entrega', description: 'Conheça nossos produtos disponíveis a pronta entrega')

You can also use iterations in the same example:

5.times do |i|
  Product.create(name: "Product ##{i}", description: "A product.")
end

To apply your popularization simply enter your terminal in the folder of your project with the command:

rake db:seed

There are people who include these popularizations in Migrations also ...

class AddInitialProducts < ActiveRecord::Migration
  def up
    5.times do |i|
      Product.create(name: "Product ##{i}", description: "A product.")
    end
  end

  def down
    Product.delete_all
  end
end
    
23.06.2014 / 17:47