ruby - Rails not including timestamps even though I specifically ask for them -
i having problem ror. need fields created_at , updated_at in in tables further use. rails doesn't seem add them though ask rails to. these models:
picture.rb class picture < activerecord::base belongs_to :category has_many :comments, dependent: :destroy mount_uploader :photo, photouploader validates_presence_of :photo end category.rb class category < activerecord::base has_many :pictures, dependent: :destroy validates :name, presence: true, uniqueness: { case_sensitive: false } end comment.rb class comment < activerecord::base belongs_to :user belongs_to :picture validates :text, presence: true end
these migrations:
class createcategories < activerecord::migration def change create_table :categories |t| t.string :name t.timestamps end end end class createpictures < activerecord::migration def change create_table :pictures |t| t.string :description t.references :category, index: true t.timestamps end add_foreign_key :pictures, :categories end end class addphototopictures < activerecord::migration def change add_column :pictures, :photo, :string end end class createcomments < activerecord::migration def change create_table :comments |t| t.string :text t.references :user, index: true t.references :picture, index: true t.timestamps end add_foreign_key :comments, :users add_foreign_key :comments, :pictures end end class addtimestampstopicture < activerecord::migration def change_table change_table :pictures |t| t.timestamps end end
as can see trying add 2 columns. when try seeing created_at field "undefined method created_at'"
. checking in rails c
see if fields there no, fields not exist. using pg gem . using carrierwave gem upload pictures. why rails not include these 2 columns ? here link project itself.
coorasse suggested took @ schema.
create_table "pictures", force: :cascade |t| t.string "description" t.integer "category_id" t.string "photo_file_name" t.string "photo_content_type" t.integer "photo_file_size" t.datetime "photo_updated_at" t.string "photo" end
it seems didn't update herself. why?
to force shema updated drop db , recreate it.
but aware - erase whole database.
rake db:drop && rake db:create && rake db:migrate
you can't edit schema or migration files. if adding new columns - should through new migration.
but if taking editing migrations files road you'll have (problems) drop db , recreate it.
Comments
Post a Comment