2016-01-20 29 views
0

您好我創建這個種子種子 - 如何重新初始化我的種子 - 軌道

rails = Course.create(title: "Ruby On Rails") 
models = rails.chapters.create(title: "Models") 

models.items << Lesson.create(title: "What is Active Record?", content: "Lesson content here") 

models.items << Exercice.create(title: "The Active Record pattern", content: "Exo about active record pattern") 
models.items << Exercice.create(title: "Object Relational Mapping", content: "Exo about ORM") 
models.items << Exercice.create(title: "Active Record as an ORM Framework", content: "Exo about ORM") 

models.items << Lesson.create(title: "Convention over Configuration in Active Record", content: "Lesson content here") 

models.items << Exercice.create(title: "Naming Conventions", content: "Exo about naming convention") 
models.items << Exercice.create(title: "Schema Conventions", content: "Exo about schema convention") 

models.items << Lesson.create(title: "Model summary", content: "Lesson content here") 

models.items << Exam.create(title: "Rails Models exam", content: "Exam content here") 

puts "done" 

我已經做了rake db:seed

我的控制過程爲:

類CoursesController < ApplicationController的

def index 
    @courses = Course.all 
    end 

    def show 
    @course = Course.find(params[:id]) 
    end 

end 

我控制器章:

類ChaptersController < ApplicationController的

def show 
    @course = Course.find(params[:course_id]) 
    @chapter = @course.chapters.find(params[:id]) 
    end 

end 

我的C ontroller章:

class ItemsController < ApplicationController 

    def show 
    @course = Course.find(params[:course_id]) 
    @chapter = @course.chapters.find(params[:chapter_id]) 
    @item = @chapter.items.find(params[:id]) 
    end 
end 

而在應用程序/視圖/場/ index.html.erb

<div class="container-page"> 
    <div class="padding-page"> 
    <div class="container-fluid"> 
     <div class="row"> 
     <div class="col-xs-12 col-sm-12 col-md-12"> 
      <div class="page-progress"> 
      <h1> 
       Page en cours de réalisation 
      </h1> 

      <% @courses.each do |course| %> 
      <h2> 
      <%= link_to course.title, course %> 

      </h2> 
      <% end %> 

      </div> 
     </div> 
     </div> 
    </div> 
    </div> 
</div> 

但現在看來,題目的課程很多次,我想有隻看到一次。如何重置或銷燬或隱藏名稱相同且名稱相同的其他課程?

Here is the index iteration

如果您想了解更多的信息,我給你,但是告訴我,我能做些什麼。謝謝。

+0

似乎有其他記錄存在你'課程'模型,做'Course.all',然後用輸出更新你的問題 – VKatz

回答

1

您是否多次運行rake db:seed?如果是這種情況,請刪除,創建,遷移,重新種子數據庫。

如果將來需要更新種子並重新運行它們,請確保不要創建多個記錄。你可以通過改變你的代碼,從:

rails = Course.create(title: "Ruby On Rails") 
models = rails.chapters.create(title: "Models") 

models.items << Lesson.create(title: "What is Active Record?", content: "Lesson content here") 

到:

rails = Course.where(title: "Ruby On Rails").first_or_create 
models = rails.chapters.where(title: "Models").first_or_create 

models.items << Lesson.where(title: "What is Active Record?").first_or_create(title: "What is Active Record?", content: "Lesson content here") 

在表中找到第一個實例。如果沒有,請創建一個。

+0

謝謝你,我明白你的意思。是。 –