i am working on a simple rails project and i use paperclip to upload attachments but when ever i use delayed job gem with delayed_paperclip gem i dont see the attachment. I followed the rdoc and tried it out but my guess is that i am not running delayed job well.
my attachment.rb lookes like this
has_attached_file :file
process_in_background :file
and i added this to my database
class AddFileProessingToAttachments < ActiveRecord::Migration
def change
add_column :attachments, :file_processing, :boolean
end
end
what else do i need to do again because i have waited for a long time and i still have a missing file.
I just realized that i have to start the delayed job task with the following command.
rake jobs:work
did not know until i checked my list of rake tasks
Related
I'm starting with RoR, so I have a question about the implementation of rake tasks.
I have a rake file with a few lines that made some scheduled tasks with Twitter Gem Api.
Run the rake works properly, but I want to order my code.
Now the rake looks like this:
desc "This task is called by the scheduler"
task :update_feed => :environment do
require 'twitter'
client = Twitter::REST::Client.new do |config|
#Client configs
end
here.i.do.some.stuff
end
I'm pretty sure that have the client configs right there isn't a good idea.
On the other hand, I want to be able to save the methods within the model so I can use them elsewhere.
So, I'm trying to do this unsuccessfully:
#On tweet.rb model file:
class Tweet < ApplicationRecord
validates :tweet_id, uniqueness: true
def here_i_do_some_stuff
#do some stuff
#Plus config the client
end
end
#And the rake file
task :update_feed => :environment do
Tweet.here_i_do_some_stuff
end
And that's not working for
rake aborted!
NoMethodError: undefined method `here_i_do_some_stuff' for Tweet (call 'Tweet.connection' to establish a connection):Class
I don't really know how to do this, I tried other ways like write on other files and require them, but I think this is the correct way, but I don't know how to make it run properly.
Thanks, JR.
I've got worker which deletes all admin logs older than 1 year. I want it run once a day using gem whenever as a rake task however I'm not follow the whole logic of rake tasks. I was trying to follow this article https://medium.com/#sampatbadhe/rake-task-invoke-or-execute-419cd689c3bd how ever the big question is should I move my worker to rake file or I can I refer directly to the file where this worker is?
my worker below:
class AdminPanelLogRemoverWorker
include Sidekiq::Worker
def perform
expired_logs = AdminPanelLog.where('created_at < ?', 1.year.ago).select(:id)
expired_logs.find_in_batches do |logs_batch|
AdminPanelLog.where(id: logs_batch.map(&:id)).delete_all
end
end
end
As far I understand my schedule.rb in whenever gem should be like below:
every 24.hours do
rake 'myproject:AdminPanelLogRemoverWorker'
end
It's a worker, not a rake task, so you can do it with
every 24.hours, at: '0:00am' do
runner 'AdminPanelLogRemoverWorker.perform_async'
end
I am still new to rails 3 and the different commands to use in terminal. I have tried to use the $ rails plugin install git://github.com/thoughtbot/paperclip.git command but everytime I hit enter, it just brings up my options when I use rails new, like -v tell your the version or -b is the builder. I don't know whats wrong
Agree with the above, gem is the way to go. Just add to you Gemfile:
gem 'paperclip', '2.3.3'
and run:
bundle install
Should be as easy as that to get paperclip running. Don't forget to include the 3 migration parts when binding to a model (example for video). In this case I am adding paperclip as 'attachment' to my Video model. Just slap 'file_name', 'content_type', and 'file_size' to the end of the downcased attribute:
class AddVideoAttachment < ActiveRecord::Migration
def self.up
add_column :videos, :attachment_file_name, :string
add_column :videos, :attachment_content_type, :string
add_column :videos, :attachment_file_size, :integer
end
def self.down
remove_column :videos, :attachment_file_size
remove_column :videos, :attachment_content_type
remove_column :videos, :attachment_file_name
end
end
I recommend you to install Paperclip as a gem, not as a plugin. Installing as a plugin can lead to all sorts of issues.
GitHub paperclip sites states specifically:
"Paperclip is distributed as a gem, which is how it should be used in your app. It's technically still installable as a plugin, but that's discouraged, as Rails plays well with gems."
Visit GitHub/Paperclip and install Paperclip as a gem and you should be fine.
I am using resque in my application for delayed jobs, where i cant send emails & sms to bulk number of users asynchronously. And the data is stored in mongodb, mongoid is the ODM connects rails & mongo.
My mongoid model looks like this
class Item
include Mongoid::Document
include Geo::LocationHelper
field :name, :type => String
field :desc, :type => String
#resque queue name
#queue = :item_notification
#resque perform method
def self.perform(item_id)
#item = Item.find(item_id)
end
end
I can able to add jobs to resque, i have verified using resque-web. Whenever i start an resque-worker
QUEUE=item_notification rake resque:work
i got the uninitialized constant Item , since i am using resque as rails gem and starting rake in rails root, i believe my mongoid models should be loaded.
After digging lot, i found that we can explicitly ask rake to load the environment by
QUEUE=item_notification rake environment resque:work
but now also i got the same error uninitialized constant Item
can someone help me out?
and my
Actually, its a problem in dev environment. after adding this line to into resque.rake task file
# load the Rails app all the time
namespace :resque do
puts "Loading Rails environment for Resque"
task :setup => :environment
ActiveRecord::Base.send(:descendants).each { |klass| klass.columns }
end
it works fine
The code taken from GitHub-Resque-Wiki
During some recent refactoring we changed how our user avatars are stored not realizing that once deployed it would affect all the existing users. So now I'm trying to write a rake task to fix this by doing something like this.
namespace :fix do
desc "Create associated ImageAttachment using data in the Users photo fields"
task :user_avatars => :environment do
class User
# Paperclip
has_attached_file :photo ... <paperclip stuff, styles etc>
end
User.all.each do |user|
i = ImageAttachment.new
i.photo_url = user.photo.url
user.image_attachments << i
end
end
end
When I try running that though I'm getting undefined method `has_attached_file' for User:Class
I'm able to do this in script/console but it seems like it can't find the paperclip plugin's methods from a rake task.
the rake task is probably not be loading the full Rails environment. You can force it to do so by doing something like this:
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
where the path leads to your environment.rb file. If this were to fix the issue, you should include it inside this task specifically, because you probably do not want all your rake tasks to include the environment by default. In fact, a rake task may not even be the best place to do what you're trying to do. You could try creating a script in the script directory as well.