これについてはすでに多くのトピックがあることは知っていますが、私がやろうとしていることを見つけることができませんでした。私はちょうど Rails を学んでいます。これはおそらく非常に単純な修正であることはわかっていますが、困惑しています。
「タイムライン」サイトを作成しています。ユーザー アカウントが設定されており、ユーザーはタイムラインを作成できます。しかし、私がする必要があるのは、複数のタイムライン「イベント」(タイムラインに移動するアイテム、これらのモデルは Event と呼ばれます) を各タイムライン (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
つまり、データベースに何かが欠けていることを意味しますが、すべてを機能させるために何を変更/追加する必要があるかわかりません。
追加情報/コードが必要な場合はお知らせください。前もって感謝します。