将一个表的条目与另一个表同步

时间:2016-03-12 17:09:48

标签: ruby-on-rails ruby

我想要从我的桌子" station"要自动复制到我的表格中#14; arrival_station"。每当我在我的工作台中创建一个新工作站时,也应该在我的arrival_station表中复制/创建这个新工作站,当我删除一个工作站时,它也会在我的arrival_station表中被删除。 anyboy可以告诉我如何解决这个问题? e.g。

station      arrival_station
---------    ---------
 id           id
 name     =>  name
 shortcut =>  shortcut

这是我的schema.rb:

create_table "stations", force: :cascade do |t|
t.string   "shortcut"
t.string   "name"
end

create_table "arrival_stations", force: :cascade do |t|
t.string   "name"
t.string   "shortcut"
t.integer "station_id"
end

我的电台控制器:

 class StationsController < ApplicationController
   before_action :set_station, only: [:show, :edit, :update, :destroy]

   def index
     @stations = Station.all
   end

   def show
     @station =Station.find(params[:id])

     respond_to do |format|
       format.html # show.html.erb
       format.json { render json: @station }
     end
   end

   def new
     @station= Station.new
  end


   def edit
   end

   def create
     @station = Station.new(station_params)

     respond_to do |format|
       if @station.save
         format.html { redirect_to @station, notice: 'Station wurde      erfolgreich erstellt.' }
         format.json { render :show, status: :created, location: @station }

       else
         format.html { render :new }
         format.json { render json: @station.errors, status:      :unprocessable_entity }
       end
     end
   end

   def update
     @station = Station.find(params[:id])

     respond_to do |format|
       if @station.update(station_params)
         format.html { redirect_to @station, notice: 'Station wurde      erfolgreich aktualisiert.' }
         format.json { respond_with_bip(@station)}
       else
         format.html { render :edit }
         format.json { render json: @station.errors, status:      :unprocessable_entity }
       end
     end
   end

   def destroy
     @station = Station.find(params[:id])

     @station.destroy
     respond_to do |format|
       format.html { redirect_to stations_url, notice: 'Station wurde      erfolgreich gelöscht.' }
       format.json { head :no_content }
     end
   end

   private

       @station = Station.find(params[:id])
     end

   def station_params
     params.require(:station).permit(:shortcut, :name)
   end
 end

和我的模特:

  class Station < ActiveRecord::Base
  has_many :arrival_stations, :dependent => :destroy

    before_save
    validates :name, presence: true, length:{ maximum: 255 },
              uniqueness: { case_sensitive: false }
    validates_format_of :name, :with =>  /\A[a-zA-Z,Ä,ä,Ü,ü,Ö,ö]+\z/i, :message       => "darf nur aus Buchstaben bestehen."
    validates :shortcut, presence: true, length: { maximum: 3 , minimum: 2 },
              uniqueness: { case_sensitive: false }
    validates_format_of :shortcut, :with =>  /\A[a-zA-Z,Ä,ä,Ü,ü,Ö,ö]+\z/i,       :message => "darf nur aus Buchstaben bestehen."
  end

  class ArrivalStation < ActiveRecord::Base
  belongs_to :station
  end

1 个答案:

答案 0 :(得分:0)

我对你的模特有点困惑,一个电台有很多到达地点,但它们都有相同的名字?你确定这不应该是has_one吗?此外,将数据存储在必须保持相同的两个地方几乎绝不是一个好主意。最有可能做的事情更好:

@arrival_station.station.name

但不管那些选择......

您当然可以在创建父项时创建子记录,并设置名称和/或快捷方式的值。

class Station < ActiveRecord::Base
  has_many :arrival_stations, :dependent => :destroy

  before_create :build_arrival_station
  validates :name, presence: true, length:{ maximum: 255 },
            uniqueness: { case_sensitive: false }
  validates_format_of :name, :with =>  /\A[a-zA-Z,Ä,ä,Ü,ü,Ö,ö]+\z/i, :message       => "darf nur aus Buchstaben bestehen."
  validates :shortcut, presence: true, length: { maximum: 3 , minimum: 2 },
            uniqueness: { case_sensitive: false }
  validates_format_of :shortcut, :with =>  /\A[a-zA-Z,Ä,ä,Ü,ü,Ö,ö]+\z/i,       :message => "darf nur aus Buchstaben bestehen."

  private
  def build_arrival_station
    arrival_stations.build(name: self.name)
  end
end