每次加载index.erb时,ActiveRecord都会创建新对象

时间:2014-11-11 17:58:22

标签: ruby activerecord sinatra

我正在使用Sinatra和ActiveRecord创建一个简单的wiki网站。我遇到一个问题,每次我尝试加载特定文档版本的索引视图时,都会创建一个新版本。我已经看过我的路线和我的版本模型,但似乎无法弄明白,虽然它可能是一些明显的东西(我是初学者)。任何帮助,将不胜感激 :) 一些相关的代码:

VERSIONS_CONTROLLER.rb

# ===============
#    Versions
# ===============

# INDEX

get '/documents/:document_id/versions' do 
    @document = Document.find(params[:document_id])
    @versions = Version.where(["document_id = ?", params[:document_id]])
    erb :'versions/index'
end

# NEW
get '/documents/:id/versions/new' do 
    @authors = Author.all()
    @document = Document.find(params[:id])
    erb :'versions/new'
end

# SHOW
get '/documents/:document_id/versions/:id' do 
    # @versions = Version.find(params[:document_id])
    @document=Document.find(params[:document_id])
    @version = Version.find(params[:id])
    erb :'versions/show'
end

# CREATE 
post '/documents/:document_id/versions' do 
    version = Version.new(params[:version])
    document.update(body: version.content)
    version.save
    redirect("/documents/#{ params[:document_id] }")
end

# REVERT

# The revert function is going to post to
# the create function above. It is going to 
# do this via a revert button under each 
# button on the versions/index.erb page.
# When pressed, the button will post a 
# /documents/:document_id/versions form
# with the params[:document_id][:version]
# set to equal those of the version under
# which the button is located

版本/ INDEX.ERB

<div class="versions-index-wrapper">
    <div class="versions-index-header">
        <h1>
            Version history for document: <%= @document.title  %>
        </h1>
    </div>
    <div class="versions-index-list">
            <% @versions.each do |version| %>
                <li>
                    <a href="/documents/<%= @document.id %>/versions/<%= version.id %>">
                    COMMIT: <%= version.blurb %><hr>
                    CREATED AT:<%= version.created_at %><hr>    BY:
                    <%= version.author.username %><hr>
                    </a>
                </li>
            <% end %> 
    </div>
</div>.

版本模型

class Version < ActiveRecord::Base
    belongs_to :document
    belongs_to :author
    has_many :comments, dependent: :destroy

    def self.latest
        self.order("created_at DESC")
    end

end

SCHEMA

DROP TABLE IF EXISTS comments CASCADE;
DROP TABLE IF EXISTS versions CASCADE;
DROP TABLE IF EXISTS documents CASCADE;
DROP TABLE IF EXISTS authors;

CREATE TABLE authors (
    id SERIAL PRIMARY KEY,
    first_name VARCHAR(255),
    last_name VARCHAR(255),
    username VARCHAR(255),
    points INTEGER,
    icon_url VARCHAR(255),
    github_url VARCHAR(255),
    twitter_url VARCHAR(255)
);

CREATE TABLE documents (
    id SERIAL PRIMARY KEY,
    title VARCHAR(255),
    img_url VARCHAR(255),
    created_at TIMESTAMP,
    updated_at TIMESTAMP,
    author_id INTEGER,
    body TEXT
);

CREATE TABLE versions (
    id SERIAL PRIMARY KEY,
    blurb TEXT,
    content TEXT,
    created_at TIMESTAMP,
    document_id INTEGER references documents,
    author_id INTEGER references authors
);

CREATE TABLE comments (
    id SERIAL PRIMARY KEY,
    created_at TIMESTAMP,
    version_id INTEGER references versions,
    author_id INTEGER references authors,
    content VARCHAR(512)
);

DOCUMENTS.rb

class Document < ActiveRecord::Base
    has_many :versions, dependent: :destroy
    has_many :authors, through: :versions
    after_initialize :init

    def init
        last_updated = Time.now.utc.iso8601.gsub('-', '').gsub(':', '')
         self.versions << Version.create({ 
             content: "version 1.0",
             blurb: "v1.0",
             author_id: Author.first.id
             })
    end

    def self.latest
        self.order("updated_at DESC")
    end

    def self.alphabetical
        self.order("title ASC")
    end

end

1 个答案:

答案 0 :(得分:0)

每次将ActiveRecord对象加载到Rails中时,after_initialize回调都会运行。由于您的应用是在每次请求时加载文档,因此每次都会调用init方法,生成新版本。