Failure to run db:migrate in Rails - ruby-on-rails

I'm trying to run db:migrate after installing image attachment via paperclip gem and it won't allow me to do migration. Could someone help please? Thanks a lot This is what it said on my terminal
This is my config file config/database.yml
This is my create item table:
class CreateTodoItems < ActiveRecord::Migration[5.0]
def change
create_table :todo_items do |t|
t.column :content
t.column :deadline
t.references :todo_list, foreign_key: true
t.timestamps
end
end
end
Item model
class TodoItem < ActiveRecord::Base
belongs_to :todo_list
has_attached_file :image, styles: { medium: "500x500>", thumb: "100x100#"}
validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
def completed?
!completed_at.blank?
end
end

You're adding a column to database that doesn't exist. You don't happen to have items table you have todo_items table your migration should look something like:
$ bin/rails generate migration AddAttachmentImageToTodoItems attachment_image:string

Related

Why is my multiple file uploader (using Carrierwave and Cloudinary) uploading the files ok but not attaching all the files to the model/table?

I am building a marketplace web app, and in doing so I need multiple product images per product.
I am using Carrierwave uploader and Cloudinary to manage the images, and to the best of my knowledge I have followed the documentation correctly.
Here are some of the key code...
From the schema, creating the product table (I have tried both with and without):
create_table "products", force: :cascade do |t|
(...)
t.json "photos", default: []
In the simple_form:
<%= f.file_field :photos, multiple: true %>
<%= f.input :photos_cache, as: :hidden %>
In the Product model:
class Product < ApplicationRecord
mount_uploaders :photos, PhotoUploader
In the PhotoUploader
class PhotoUploader < CarrierWave::Uploader::Base
include Cloudinary::CarrierWave
end
In the controller, the strong params:
def new_product_params
params.require(:product).permit( (...) {photos: []})
The form is posted and the new instance saved, all the files are uploaded to Cloudinary ok, but only the first image of all selected gets saved to the instance, and in a string - not the expected hash/json.
From the console - before:
photos: []>
And after:
photos: "image/upload/v1545381249/fdw1ydn6d1latvtlbobr.jpg">
I have seen other tutorials that have the image field as only an array and not a json, but Carrierwave documentation says to create a json field type.
Hope this can help you
In your Product Model :
...
mount_uploader :photos, PhotoUploader
serialize :photos, JSON
...
Create another migration script :
def change
remove_column :products, :photos
add_column :products, :photos, :string, array: true, default: []
end

ActiveModel::UnknownAttributeError in rails 5

I'm trying to make a reddit-like website, so I've created a Forum model which should contain posts, hence I also created a Post model which I want to be a Forum child. My code for this idea is:
forum.rb
class Forum < ApplicationRecord
has_many :posts
validates :name, presence: true,
length: { minimum: 2 }
end
post.rb
class Post < ApplicationRecord
validates :title, presence: true,
length: { minimum: 5 }
validates :text, presence: true,
length: { minimum: 5 }
belongs_to :forum
end
The corresponding migrations are:
create_forums.rb
class CreateForums < ActiveRecord::Migration[5.1]
def change
create_table :forums do |t|
t.string :name
t.text :description
t.timestamps
end
end
end
create_posts.rb
class CreatePosts < ActiveRecord::Migration[5.1]
def change
create_table :posts do |t|
t.string :title
t.text :text
t.references :forum, index: true, foreign_key: true
t.timestamps
end
end
end
My problem arise when I try to create a new post given a certain forum. For example, if I run #forum.posts.create(title: "Title", text: "Body") I get ActiveModel::UnknownAttributeError in Posts#new with the description unknown attribute 'forum_id' for Post.
What is going on?
Have you run your migrations after generating them? ( rake db:migrate )
The only way I can get this error to occur in testing is to remove the forum reference/relationship field from the posts table and then try and create a post related to the forum. When the migration is run with the t.references :forum, index: true, foreign_key: true the command runs perfectly for me.
If you added the reference line after running the migration, you could reset your database by running rake db:drop db:create db:migrate and you should be good to go since you have it in the table creation migration file, but it is worth noting that if you want to add or remove columns or modify your database, you should be creating new migrations for this instead of running through the drop/create process.
Have you migrated your db? If no, then rails db:migrate
and then reset your db: rails db:setup
That should fix the issue.

Rails4 : My hstore attribute is being converted to String

I have a Hash attribute in my model that uses Postgres hstore extention. The problem is that this attribute is converted to String by Rails4. This prevents me to make basic operations such as .each or .map to treat my hash attribute.
Using the Rails console, the Hash is not converted. Typing:
#device.data
#device.data.class
Gives in Rails console:
{"city"=>"London", "owner_name"=>"John"}
Hash
And in the application itself (using the navigator):
"\"city\"=>\"London\","\"owner_name\"=>\"John\"
String
Do you have any idea?
Update:
Here is the model:
class Device < ActiveRecord::Base
belongs_to :company
has_many :records
validates :name, presence: true
end
And the corresponding migration file:
class CreateDevices < ActiveRecord::Migration
def change
create_table :devices do |t|
t.string :name
t.hstore :data
t.integer :company_id
t.timestamps
end
add_index :devices, :name
end
end
Try deleting your tmp folder and restarting all your servers.
rm -rf tmp/*

Rails/Paperclip object creation issue

I am having a problem while validating on an image field in model on a rails4 app.
class ModelA < ActiveRecord::Base
validates :name, presence: true
validates :logo, presence: true
has_attached_file :logo, styles: {
thumb: '100x100>',
square: '200x200#'
}
In the migrations, a new instance of this model is to be created.
def migrate(direction)
super
if direction == :up
obj = Model1.create!(:name => "Test")
This is failing as the required field is not specified and If I am explicitly specifying a default image, then the table does not have the necessary column yet.
This migration runs if I am removing the image (in this case, logo) validation before migration and thereafter specify the image file and details like its name.
Is there a better way to setup this model?
I've figured this out. The problem was in the migrations - the logo migration (as well as validation) was added afterwards of this Object creation in the migration. I added the logo to the Model1.create! and moved this migration After these migrations to solve the error.
Hence, my migrations roughly are:
def change
create_table :... do |t|
t.string :name
t.timestamps
end
Followed by addition of paperclip column
def self.up
change_table :... do |t|
t.attachment :logo
end
end
And added the model in another migration that was coming after them.
def migrate(direction)
super
if direction == :up
logo_img = File.open('app/assets/images/logo-big.png', 'rb')
Model1.create!(:name => "TestObj", :logo => logo_img )

Why isn't this rails file attachment (using dragonfly) loading?

I have been at this one for a while and hitting a great wall of fail...
I'm trying to pull some game info from Mochi Media's servers. I have the API hooks in place but when I try to access the file info with Dragonfly I'm getting the following error:
undefined method `file_uid' for #<Game:0x007fba5e0bf188>
Can anyone see anything weird that I'm doing?
Model:
class Game < ActiveRecord::Base
attr_accessible :description, :name, :file, :image
image_accessor :image
image_accessor :file
validates :name, uniqueness: true
validates :name, presence: true
end
Class:
def move(game)
# Grab the mochi game from the queue
mochi_game = Mochi.find(game.id)
if mochi_game
# Setup a new game object
game = Game.new
# Pull the game's name
game.name = mochi_game.name
# Pull the thumb & game file
game.image_url = mochi_game.image_url
game.file_url = mochi_game.file_url
# Pull the metadata
game.description = mochi_game.description
# Save the game to our db
game.save
end
return false
end
Migration:
class AddImageAndGametoGames < ActiveRecord::Migration
def up
add_column :games, :image_uid, :string
add_column :games, :file_uid, :string
end
def down
remove_column :games, :image_uid, :string
remove_column :games, :file_uid, :string
end
end
Looks like once again, in pure Ruby/Rails newbie fashion, I managed to get this wrong. The issue was stemming from a few crazy issues with my DB schema. Once I worked all that out and re-installed the plugin based on an older GIT commit I was able to get this gem up and running.
Hopefully, in some odd way, this helps another SO user who may be interested in trying this Gem.

Resources