Rails Paperclip plugin stopped working? - ruby-on-rails

I am using the Paperclip plugin to manage file uploads to my application. For some reason in the last day or so the plugin/model has stopped working and now returns the following error message:
Paperclip::PaperclipError in DeliversController#create
Asset model missing required attr_accessor for 'data_file_name'
As far as I am aware, I haven't touched the delivers controller or the paperclip plugin.
Has anyone seen this error before, or know how I can trace the last change on the file thats error'ing?
For reference the db schema is as follows:
# Create Delivers Table
create_table :delivers do |t|
t.column :caseref, :string
t.column :casesubject, :string
t.column :description, :text
t.column :document_file_name, :string
t.column :document_content_type, :string
t.column :document_file_size, :integer
t.column :document_updated_at, :datetime
t.timestamps
end
# Create Assets Table
create_table :assets do |t|
t.column :attachable_id, :integer
t.column :attachable_type, :string
t.column :date_file_name, :string
t.column :date_content_type, :string
t.column :date_file_size, :integer
t.column :attachings_count, :integer, :default => 0
t.column :created_at, :datetime
t.column :date_updated_at, :datetime
t.timestamps
end
and the asset model is as follows:
class Asset < ActiveRecord::Base
has_attached_file :data,
:url => "/assets/:id",
:path => ":rails_root/assets/docs/:id/:style/:basename.:extension"
belongs_to :attachable, :polymorphic => true
def url(*args)
data.url(*args)
end
def name
data_file_name
end
def content_type
data_content_type
end
def file_size
data_file_size
end
end
Thanks,
Danny

# Create Assets Table
t.column :date_file_name, :string
^^^
class Asset < ActiveRecord::Base
has_attached_file :data,
^^^
See the difference? Once it is datE and than it's datA

Just try changing this
#Create Assets Table
create_table :assets do |t|
t.column :attachable_id, :integer
t.column :attachable_type, :string
t.column :date_file_name, :string
t.column :date_content_type, :string
t.column :date_file_size, :integer
t.column :attachings_count, :integer, :default => 0
t.column :created_at, :datetime
t.column :date_updated_at, :datetime
t.timestamps
end
to this
# Create Assets Table
create_table :assets do |t|
t.column :attachable_id, :integer
t.column :attachable_type, :string
t.column :data_file_name, :string
t.column :data_content_type, :string
t.column :data_file_size, :integer
t.column :attachings_count, :integer, :default => 0
t.column :created_at, :datetime
t.column :date_updated_at, :datetime
t.timestamps
end
I think the error message indicates it as
Asset model missing required attr_accessor for 'data_file_name'

Related

adding multipe new columns in rails migration using block

Is there any better way to add new columns to rails table than this way
class AddColumnsToUsers < ActiveRecord::Migration[5.1]
def change
add_column :users, :first_name, :string
add_column :users, :last_name, :string
add_column :users, :contact1, :integer
add_column :users, :contact2, :integer
add_column :users, :contact3, :decimal
add_column :users, :contact4, :integer
add_column :users, :contact5, :integer
add_column :users, :contact6, :string
add_column :users, :contact7, :integer
add_column :users, :contact8, :integer
add_column :users, :contact9, :integer
end
end
Can we use change_table method and write these inside a block? instead of repeating the add_column again and again
You can add multiple columns to a same table like this
def change
change_table :users do |t|
t.string :first_name
t.string :last_name
end
end
If you just want to dry, then you can write in following manner also,
{
string: [:first_name, :last_name, :contact6],
integer: [:contact1 ,:contact2 ,:contact4 ,:contact5 ,:contact7 ,:contact8 ,:contact9],
decimal: [:contact3]
}.each do |type, columns|
columns.each { |col| add_column :users, col, type }
end

rake db:migrate issue postgresql

I'm complete noob at PostgreSQL, Ruby on Rails..
I'm trying to follow this tutorial (WITHOUT rubymine) http://www.codeproject.com/Articles/575551/User-Authentication-in-Ruby-on-Rails#InstallingRubyMine4
I have as such this to migrate (001_create_user_model.rb):
class CreateUserModel < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.column :username, :string
t.column :email, :string
t.column :password_hash, :string
t.column :password_salt, :string
end
end
def self.down
drop_table :users
end
end
The error I'm getting goes like this:
syntax error, unexpected ':', expecting ';' or '\n'
t.column...sers do |t|
...
C:131071:in 'disable_dll_transaction'
Task:TOP => db:migrate
What about this:
class CreateUserModel < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :username, :null => false
t.string :email,:null => false
t.string :password_hash, :null => false
t.string :password_salt, :null => false
t.timestamps
end
end
def self.down
drop_table :users
end
end

add column to mailboxer migration

I wanted to add two columns to the migration that makes the notifications for the mailboxer gem. When I place both in the migration that creates the notification and then run a migration it goes through. I then input them into the form and when I submit them there are no errors and the log shows that its take them. For some reason when I try to display them they don't show up, the only thing that is showing is the original things that were part of the migration which is subject and body. My question is how can I add columns to this migration?
Here is the migration with my two columns of lat and long added to the notifications section.
# This migration comes from mailboxer_engine (originally 20110511145103)
class CreateMailboxer < ActiveRecord::Migration
def self.up
#Tables
#Conversations
create_table :conversations do |t|
t.column :subject, :string, :default => ""
t.column :created_at, :datetime, :null => false
t.column :updated_at, :datetime, :null => false
end
#Receipts
create_table :receipts do |t|
t.references :receiver, :polymorphic => true
t.column :notification_id, :integer, :null => false
t.column :read, :boolean, :default => false
t.column :trashed, :boolean, :default => false
t.column :deleted, :boolean, :default => false
t.column :mailbox_type, :string, :limit => 25
t.column :created_at, :datetime, :null => false
t.column :updated_at, :datetime, :null => false
end
#Notifications and Messages
create_table :notifications do |t|
t.column :type, :string
t.column :body, :text
t.column :subject, :string, :default => ""
t.column :lat, :text
t.column :long, :text
t.references :sender, :polymorphic => true
t.references :object, :polymorphic => true
t.column :conversation_id, :integer
t.column :draft, :boolean, :default => false
t.column :updated_at, :datetime, :null => false
t.column :created_at, :datetime, :null => false
end
#Indexes
#Conversations
#Receipts
add_index "receipts","notification_id"
#Messages
add_index "notifications","conversation_id"
#Foreign keys
#Conversations
#Receipts
add_foreign_key "receipts", "notifications", :name => "receipts_on_notification_id"
#Messages
add_foreign_key "notifications", "conversations", :name => "notifications_on_conversation_id"
end
def self.down
#Tables
remove_foreign_key "receipts", :name => "receipts_on_notification_id"
remove_foreign_key "notifications", :name => "notifications_on_conversation_id"
#Indexes
drop_table :receipts
drop_table :conversations
drop_table :notifications
end
end
My show view looks like
%h1= conversation.subject
%ul
= content_tag_for(:li, conversation.receipts_for(current_user)) do |receipt|
- message = receipt.message
%h3= message.subject
%p= message.body
%p= message.lat
%p= message.long
= render 'messages/form', conversation: conversation
This is what comes up in the console, for lat and long you see it says null
Notification Load (0.1ms) SELECT "notifications".* FROM "notifications" ORDER BY "notifications"."id" DESC LIMIT 1
--- !ruby/object:Message
attributes:
id: 4
type: Message
body: game
subject: wiz
lat: !!null
long: !!null
sender_id: 1
sender_type: User
conversation_id: 2
draft: false
updated_at: 2013-03-10 04:37:54.984277000Z
created_at: 2013-03-10 04:37:54.984277000Z
notified_object_id: !!null
notified_object_type: !!null
notification_code: !!null
attachment: !!null
=> nil
I am not sure I understand exactly what you are doing, but it sounds like possibly you haven't added the two new columns to attr_accessible and they aren't being saved because of that. That's the first thing I would check (it's in the model).
Otherwise, go to the console and see if your new columns are there and see if there is data in them. That will help you find where the problem is.

Ruby on Rails DB Migration Script Using Text instead of :string varchar

I'm a beginner at Ruby on Rails so I apologize if this is quite obvious, but I'm trying to learn how to write the database migration scripts and I'd like to change the following long_description to a text value instead of string:
class CreateArticles < ActiveRecord::Migration
def self.up
create_table :articles do |t|
t.column "short_description", :string
t.column "long_description", :string
t.timestamps
end
end
end
Any ideas how this is possible?
class CreateArticles < ActiveRecord::Migration
def self.up
create_table :articles do |t|
t.string :short_description
t.text :long_description
t.timestamps
end
end
def self.down
# don't forget the down method
end
end
Also, don't forget the down method.
Migration types are listed here.
:string
:text
:integer
:float
:decimal
:datetime
:timestamp
:time
:date
:binary
:boolean
create_table :articles do |t|
t.column 'long_description', :text
# ...
end
Set it to :text
Here's a good ref for you: Here

When I run the rake:db migrate command I get an error "Uninitialized constant CreateArticles"

I created a model ruby script/generate model Article (simple enuff)
Here is the migration file create_articles.rb:
def self.up
create_table :articles do |t|
t.column :user_id, :integer
t.column :title, :string
t.column :synopsis, :text, :limit => 1000
t.column :body, :text, :limit => 20000
t.column :published, :boolean, :default => false
t.column :created_at, :datetime
t.column :updated_at, :datetime
t.column :published_at, :datetime
t.column :category_id, :integer
end
def self.down
drop_table :articles
end
end
When I run the rake:db migrate command I receive an error rake aborted! "Uninitialized constant CreateArticles."
Does anyone know why this error keeps happening?
Be sure that your file name and class name say the same thing(except the class name is camel cased).The contents of your migration file should look something like this, simplified them a bit too:
#20090106022023_create_articles.rb
class CreateArticles < ActiveRecord::Migration
def self.up
create_table :articles do |t|
t.belongs_to :user, :category
t.string :title
t.text :synopsis, :limit => 1000
t.text :body, :limit => 20000
t.boolean :published, :default => false
t.datetime :published_at
t.timestamps
end
end
def self.down
drop_table :articles
end
end
It's possible to get the given error if your class names don't match inflections (like acronyms) from config/initializers/inflections.rb.
For example, if your inflections include:
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.acronym 'DOG'
end
then you might need to make sure the class in your migration is:
class CreateDOGHouses < ActiveRecord::Migration[5.0]
rather than:
class CreateDogHouses < ActiveRecord::Migration[5.0]
Not super common, but if you generate a migration or a model or something, and then add part of it to inflections afterwards it may happen. (The example here will cause NameError: uninitialized constant CreateDOGHouses if your class name is CreateDogHouses, at least with Rails 5.)
If you're getting this error and it's NOT because of the migration file name, there is another possible solution. Open the class directly in the migration like this:
class SomeClass < ActiveRecord::Base; end
It should now be possible to use SomeClass within the migration.
The top answer solved for me. Just leaving this here in case it helps.
Example
If your migration file is called
20210213040840_add_first_initial_only_to_users.rb
then the class name in your migration file should be
AddFirstInitialOnlyToUsers
Note: if the class name doesn't match, it will error even if the difference is just a lower case t instead of an upper case 'T' in 'To' - so be careful of that!

Resources