1

我知道已经有很多关于此的主题,但我找不到任何我正在尝试做的事情。我只是在学习 Rails,虽然我知道这可能是一个非常简单的解决方法,但我很难过。

我正在创建一个“时间轴”网站。我设置了用户帐户,用户可以创建时间线。但是,我需要做的是将多个时间线“事件”(要进入时间线的项目,这些模型称为事件)与每个时间线(其模型称为 Timeline_Object)相关联。更简单地说——一个用户有多个时间线,一个时间线有多个事件。

问题是我无法使用时间线正确设置事件。我认为用户和时间线之间的关联设置正确,但我不完全确定如何找出问题所在。这是我的模型:

class User < ActiveRecord::Base

  has_many :timeline_objects

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, 
  :remember_me, :user_name

end


class TimelineObject < ActiveRecord::Base

    belongs_to :user

    attr_accessible :title, :user_id

    has_many :events    

end


class Event < ActiveRecord::Base
    belongs_to :timeline_object

    attr_accessible :date, :description, :time, :title, :image, 
    :image_width, :image_height, :timeline_objects
    has_attached_file :image, :styles => { :large => "500x500>", :medium => "400x400#", :thumb => "100x100>" }
    after_post_process :save_image_dimensions

    validates :title, presence: true
    validates :image, presence: true
    validates :time, presence: true
    validates :date, presence: true 

  def save_image_dimensions
    geo = Paperclip::Geometry.from_file(image.queued_for_write[:original])
    self.image_width = geo.width
    self.image_height = geo.height
  end
end

在运行一些迁移以在数据库中设置密钥之后,我的架构如下所示:

ActiveRecord::Schema.define(:version => 20130402144923) do

  create_table "events", :force => true do |t|
    t.string   "title"
    t.string   "description"
    t.string   "date"
    t.string   "time"
    t.datetime "created_at",         :null => false
    t.datetime "updated_at",         :null => false
    t.string   "image_file_name"
    t.string   "image_content_type"
    t.integer  "image_file_size"
    t.datetime "image_updated_at"
    t.integer  "image_height"
    t.integer  "image_width"
    t.integer  "timeline_objects"
  end

  add_index "events", ["timeline_objects"], :name => "index_events_on_timeline_objects"

  create_table "timeline_objects", :force => true do |t|
    t.string   "title"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
    t.integer  "user_id"
  end

  add_index "timeline_objects", ["user_id"], :name => "index_timeline_objects_on_user_id"

  create_table "users", :force => true do |t|
    t.string   "user_name"
    t.string   "email",                  :default => "", :null => false
    t.string   "encrypted_password",     :default => "", :null => false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          :default => 0
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                             :null => false
    t.datetime "updated_at",                             :null => false
  end

  add_index "users", ["email"], :name => "index_users_on_email", :unique => true
  add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true

end

当我去显示时间线时(此时应该显示所有事件),我尝试通过它们循环

<% @timeline_object.events.each do |event| %>

那行代码会产生这个错误:

SQLite3::SQLException: no such column: events.timeline_object_id: SELECT "events".* FROM "events"  WHERE "events"."timeline_object_id" = 4

所以我意识到这意味着我在我的数据库中遗漏了一些东西,但我不确定我应该更改/添加什么以使其全部正常工作。

如果您需要任何其他信息/代码,请告诉我。提前致谢。

4

1 回答 1

1

在您的事件模式中,您有:

t.integer  "timeline_objects"

但是,它应该是:

t.integer  "timeline_object_id"

运行一个新的迁移来修复它:

rename_column :events, :timeline_objects, :timeline_object_id

由于每个事件都属于一个 TimelineObject,因此它需要一个列来标识与其关联的对象的 id。

于 2013-04-02T15:31:17.473 回答