I am using RubyMine with rails 3.2.12 and I am getting following deprecated warning in my IDE. Any Idea How can I solve this deprecated warning?
find(:first) and find(:all) are deprecated in favour of first and all methods. Support will be removed from rails 3.2.
I changed my answer after #keithepley comment
#Post.find(:all, :conditions => { :approved => true })
Post.where(:approved => true).all
#Post.find(:first, :conditions => { :approved => true })
Post.where(:approved => true).first
or
post = Post.first or post = Post.first!
or
post = Post.last or post = Post.last!
You can read more from this locations
deprecated statement
Post.find(:all, :conditions => { :approved => true })
better version
Post.all(:conditions => { :approved => true })
best version (1)
named_scope :approved, :conditions => { :approved => true }
Post.approved.all
best version (2)
Post.scoped(:conditions => { :approved => true }).all
Here is the Rails 3-4 way of doing it:
Post.where(approved: true) # All accepted posts
Post.find_by_approved(true) # The first accepted post
# Or Post.find_by(approved: true)
# Or Post.where(approved: true).first
Post.first
Post.last
Post.all
Use the new ActiveRecord::Relation stuff that was added in Rails 3. Find more info here: http://guides.rubyonrails.org/active_record_querying.html
Instead of #find, use #first, #last, #all, etc. on your model, and the methods that return a ActiveRecord::Relation, like #where.
#User.find(:first)
User.first
#User.find(:all, :conditions => {:foo => true})
User.where(:foo => true).all
Related
I currently have an application using paperclip that allows users to upload their creatives. This has worked flawless thus far when it comes to a user uploading an image file. We have since tested to upload a .mov file and I get this error:
Creative Paperclip::Errors::NotIdentifiedByImageMagickError
The weird thing, this error is only generated on Heroku. I can upload .mov files just fine on my local host.
My Current Gem Setup:
paperclip (3.4.1, 3.4.0)
paperclip-aws (1.6.7, 1.6.6)
paperclip-ffmpeg (0.10.2)
cocaine (0.5.1, 0.4.2)
Event.rb
has_attached_file :creative,
:processors => [:ffmpeg],
:styles => {
:thumb => [:geometry => "250x150", :format => 'png'],
:custcreative => [:geometry => "275x75", :format => 'png'],
:creativepreview => ["275x195",:png]
},
:url => "***",
:path => "***",
:s3_domain_url => "***",
:storage => :s3,
:s3_credentials => Rails.root.join("config/s3.yml"),
:bucket => '***',
:s3_permissions => :public_read,
:s3_protocol => "http",
:convert_options => { :all => "-auto-orient" },
:encode => 'utf8'
Spending hours trying to figure out why this works locally but throwing error on Heroku.
I even tried removing the :style setting, but still did not work.
TIA
EDIT
Command :: identify -format '%wx%h,%[exif:orientation]' '/tmp/MidPen20130413-2-1mzetus.mov[0]'
Well, here is the answer in case any other newbies like us come across the same problem. The issue is with the geometry method that is being used for image cropping. The way it is suggested in railscasts assumes the file is in a local system and that needs to be changed.
OLD METHOD:
def avatar_geometry(style = :original)
#geometry ||= {}
#geometry[style] ||= Paperclip::Geometry.from_file(avatar.path(style))
end
NEW METHOD
def avatar_geometry(style = :original)
#geometry ||= {}
avatar_path = (avatar.options[:storage] == :s3) ? avatar.url(style) : avatar.path(style)
#geometry[style] ||= Paperclip::Geometry.from_file(avatar_path)
end
I'm using Redcarpet gem for markdown and i wanna to generate automatic anchors for h2 titles to allow linking to each section.
show.html.erb
<div class = "content"><%= markdown(#post.body) %></div>
application_helper.rb
class HTMLwithPygments < Redcarpet::Render::HTML
def block_code(code, language)
Pygments.highlight(code, :lexer => language)
end
end
def markdown(text)
renderer = HTMLwithPygments.new(:hard_wrap => true, :with_toc_data => true)
options = {
:fenced_code_blocks => true,
:no_intra_emphasis => true,
:autolink => true,
:strikethrough => true,
:lax_html_blocks => true,
:superscript => true,
}
Redcarpet::Markdown.new(renderer, options).render(text).html_safe
end
I read about :with_toc_data => true but it doesn't works for me. I added it in options area.
I do the same thing as you describe which basically is
Redcarpet::Markdown.new(
Redcarpet::Render::HTML.new(with_toc_data: true), {}
).render(text)
My view contains the following code
- #article.description.scan(/(#+)(.*)/).each do |menu_item|
= content_tag(:a, menu_item[1], :href => "##{menu_item[1].downcase.strip.gsub(" ","-")}")
Maybe this helps.
Can't figure out how to do this? and couldn't find much help from anywhere else!
I have set up the paperclip and fog like this;
config/initializers/fog.rb
connection = Fog::Storage.new({
:provider => 'Rackspace',
:rackspace_username => '',
:rackspace_api_key => ''
})
environment.rb;
Paperclip::Attachment.default_options.update({
:path => ":attachment/:id/:timestamp_:style.:extension",
:storage => :fog,
:fog_credentials => {
:provider => 'Rackspace',
:rackspace_username => '',
:rackspace_api_key => '',
:persistent => false
},
:fog_directory => '',
:fog_public => true
})
I am using file_field to get an image and then posting it to my controller. This gets me something like this in;
"pic"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007f90ac06a6c8 #original_filename="3245-1920x1200.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"cloth[pic][image]\"; filename=\"3245-1920x1200.jpg\"\r\nContent-Type: image/jpeg\r\n", #tempfile=#<File:/tmp/RackMultipart20130104-5386-103laem>>}
What I can't understand is that how do I go about actually saving this file to cloud files using something like this;
file = directory.files.create(
:key => 'resume.html',
:body => File.open("/path/to/my/resume.html"),
:public => true
)
EDIT
Relevant Models;
class Cloth
include Mongoid::Document
has_many :pics
class Pic
include Mongoid::Document
include Mongoid::Paperclip
belongs_to :cloth
has_mongoid_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }
And in the controller this is how I am currently creating pic based on above params;
#cloth = Cloth.new
#cloth.pics.create!(params[:cloth][:pic])
Let's step back and look at the problem from different perspective. Can you see if the following script will upload an image to your container:
require 'fog'
service = Fog::Storage.new({
:provider => 'Rackspace',
:rackspace_username => YOUR_USERNAME,
:rackspace_api_key => YOUR_API_KEY
})
container = service.directories.new(:key => YOUR_CONTAINER_NAME)
container.files.create :key => 'my-pix.jpg', :body => File.open PATH_TO_FILE
Update the uppercase parameters with the appropriate variables and let me know what happens. Hopefully this will help narrow down the problem.
Paperclip and ActiveRecord should automatically handle the file upload for you. Here is a good quick start explaining the process:
https://github.com/thoughtbot/paperclip#quick-start
If you are still having issues, can you provide me with the relevant controller and model code?
we are upgrading from rails 2.3 to rails 3 and we have some validation on a model like:
validates_length_of :corporate_type, :in => 1..255, :allow_blank => false, :on => :update, :if => Proc.new { |rra| rra.show_corporate_type? }
In rails 2.3 this only gets called on update, but in rails 3, it seems to be called on create, which breaks some stuff downstream... Can someone explain why this is getting called on create?
Here is the stack:
app/models/rra_agreement.rb:11:in `block in <class:RRAAgreement>'
app/models/foo_application_delegate.rb:29:in `create_application'
Line 28 and 29 are:
rra = RRAAgreement.new()
rra.save
line 11 is the validation line above
thanks
Joel
I would try do the following using :new_record?
validates :corporate_type, :unless => :new_record?
new_records? returns true if it is being just created, else false.
The syntax is changed for rails 3, try this to call only on update,
validates :corporate_type,:length => {:on => :update, :min => 1, :max => 20 }
Well I have a work around, its ugly, but it seems to at least let us move forward:
validates_length_of :corporate_type, :in => 1..255, :allow_blank => false, :on => :update, :if => Proc.new { |rra| return false if rra.id==nil;rra.show_corporate_type? }
This makes it just false if the id is not there,it should be there on update.
I have Rails 3.0.3 with these gems:
delayed_job 2.1.4
delayed_paperclip 0.7.1
paperclip 2.3.16
paperclip-ffmpeg 0.7.0
(This combination is very specific. Some newer gems will not work with others.)
Here's my Video model:
class Video < Upload
has_attached_file :file, :default_style => :view, :processors => [:ffmpeg],
:url => '/system/:class/:attachment/:id/:style/:basename.:extension',
:path => ':rails_root/public/system/:class/:attachment/:id/:style' \
+ '/:basename.:extension',
:default_url => '/images/en/processing.png',
:styles => {
:mp4video => { :geometry => '520x390', :format => 'mp4',
:convert_options => { :output => { :vcodec => 'libx264',
:vpre => 'ipod640', :b => '250k', :bt => '50k',
:acodec => 'libfaac', :ab => '56k', :ac => 2 } } },
:oggvideo => { :geometry => '520x390', :format => 'ogg',
:convert_options => { :output => { :vcodec => 'libtheora',
:b => '250k', :bt => '50k', :acodec => 'libvorbis',
:ab => '56k', :ac => 2 } } },
:view => { :geometry => '520x390', :format => 'jpg', :time => 1 },
:preview => { :geometry => '160x120', :format => 'jpg', :time => 1 }
}
validates_attachment_content_type :file, :content_type => VIDEOTYPES,
:if => Proc.new { |upload| upload.file.file? }
process_in_background :file
end
When creating a new Video object with attachment, the original is saved, but no conversion will be done. Even calling Video.last.file.reprocess! won't to a thing except returning true. (Not sure what "true" means in this case as it didn't work.)
I tried hardcoding the path to ffmpeg in Paperclip::options[:command_path]. I even tried deleting the paperclip-ffmpeg.rb file and replacing it with a blank file. Really thinking I'd get an exception by doing the later, instead, I simply got "true" again.
It feels like the paperclip-ffmpeg.rb is being loaded, because it is required by config/application.rb, but nothing is called in it when trying to generate a thumbnail or convert a video.
Can anyone help me with this? Thanks in advance!
Looks like I solved this problem myself, and it was caused by something I did.
I wrote my own script to import files and the database from an older app to Rails. The files were in place, but someone I updated the database with the wrong file extensions (in this case, ".jpg" instead of ".MOV").
Paperclip will verify first to see if the original file exists before calling any processor, based on the file name stored in the database. As it didn't, Paperclip just didn't do anything. Once I had the data corrected, everything ran as expected. (I had problems with FFMPEG, but that's a different issue.)
My apologies if I wasted anyone's time. Hope this can be helpful for someone.
I use a similar configuration for one of my project (but Rails 3.1.1) and everything works fine. I added paperclip-ffmpeg to my Gemfile not with config/application.rb. Maybe this helps!?