How to automatically initialize has_many attributes in Rails

Asked

Viewed 265 times

0

Currently I have 3 model

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

In the Model Tipofrete there are 5 records that will initially be system standards.

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

2 answers

1


Only to leave to those who will be in the same trouble as mine.

First I did the mapping wrong. The correct 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

inverti os has_many

To start registration it has to be at the Controller level

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

The operator << adds objects to a list or array

  • Mark your answer as correct to put it first and indicate to users that this is the answer that answers the question.

1

Rails has a standardization to popular your database, this standardization serves to maintain the maintenance of your app. I don’t know what your database columns look like, so I’ll post a generic template:

Inside your Rails project folder, you will have the following file: db/seed.rb, this is the file responsible for popular 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 it, example:

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

To apply your popularization just 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
  • Yes, I put the standard Tipofrete records on Seed, as these will be virtually fixed. My problem was in the Region of Freight because these are dynamic and can have 1..N. My question was, when creating a region automatically creates the Valorefrete, one for each Tipofrete.

  • From what you explained to you to add a Region you need to have a Pre-registered Type (Seed), correct? Using Seed you will already have the registered types and then the person selects the type for the region. That wasn’t it? :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.