为直通协会生成迁移

时间:2016-03-11 03:35:51

标签: ruby-on-rails migration

以下是我对模特的补充:

class Event < ApplicationRecord
  has_one :lineup
  has_many :artists, :through => :lineup
  belongs_to :venue
end

class Lineup < ApplicationRecord
  belongs_to :artist
  belongs_to :event
end

class Artist < ApplicationRecord
  has_many :events, :through => :lineups
end

class Venue < ApplicationRecord
  has_many :events
end

没有请求为所有这些关联生成迁移的帮助,但您是否至少可以告诉我如何为Event执行迁移?

3 个答案:

答案 0 :(得分:1)

请查看以下GLuint OpenGLCreateShader(void *ShaderContents, uint32 ShaderType) { GLuint Result = glCreateShader(ShaderType); glShaderSource(Result, 1, (GLchar **)&ShaderContents , 0); glCompileShader(Result); GLint CompileStatus; glGetShaderiv(Result, GL_COMPILE_STATUS, &CompileStatus); if(CompileStatus == GL_FALSE) { GLint InfoLogLength; glGetShaderiv(Result, GL_INFO_LOG_LENGTH, &InfoLogLength); if(InfoLogLength > 1) { GLchar *InfoLog = (GLchar *)malloc(InfoLogLength + 1); glGetShaderInfoLog(Result, InfoLogLength, &InfoLogLength, InfoLog); OutputDebugString(InfoLog); free(InfoLog); } else { // TODO(zak): Logging } } return(Result); } &amp;的迁移情况。 $(".trail").click(function() { $(".grid").mouseenter(function() { $(this).css("background-color", "black"); $(this).fadeTo(0, 0); $(this).mouseleave(function() { $(this).fadeTo(600, 1); }); }); }); (具有启用模型关联的键):

Event

要生成它们,您可以使用:

Lineup

如果class CreateEvents < ActiveRecord::Migration def change create_table :events do |t| t.references :venue, index: true, foreign_key: true t.timestamps null: false end end end class CreateLineups < ActiveRecord::Migration def change create_table :lineups do |t| t.references :artist, index: true, foreign_key: true t.references :event, index: true, foreign_key: true t.timestamps null: false end end end &amp; rails g migration create_events venue:references rails g migration create_lineups artist:references event:references 已经存在,您可以按如下方式生成迁移:

Event

生成的迁移应如下所示:

Lineup

答案 1 :(得分:1)

belongs_to会将外键放在声明模型中,而has_one会将其放在另一个模型中。这里有很好的资源,我建议你去看看。这是one

因此,对于事件模型,我会执行以下操作:

$ rails g migration AddVenueToEvents

然后填写:

class AddVenueToEvents < ActiveRecord::Migration
  def change
    add_reference :events, :venue, index: true, foreign_key: true
  end
end

我强烈建议结合RSpec使用像Shoulda gem这样的东西,因为它提供了关于你应该做什么的非常有价值的反馈。这将允许你写一些规格:

RSpec.describe Events, type: :model do

  #Associations
  it { should belong_to(:venue) }
  it { should have_one(:lineup) }
  it { should have_many(:artists).through(:lineup) }

令人敬畏的是,一旦你运行你的规范,shoulda / rspec将在终端中为你提供非常有用的反馈,基本上告诉你可能缺少所需外键的位置。消息可能如下所示:

Region should have a city
     Failure/Error: should belong_to(:city)
       Expected Region to have a belongs_to association called city (Region does not have a city_id foreign key.)
     # ./spec/models/region_spec.rb:5:in `block (2 levels) in <top (required)>'

如此other SO post所示,这有点相关。

答案 2 :(得分:0)

请检查events的此迁移。

class CreateEvents < ActiveRecord::Migration
  def change
    create_table :events do |t|
      t.belongs_to :venue, index: true
      t.timestamps null: false
    end
  end
end