I have rather weird problem with paperclip gem. You know it defines Attachment class inside itself. So the model with exactly the same name already exists in my project. As the result in some parts of the code i cant get access to my previous Attachment model.
I tried to write full name of my model class, but the result was very interesting (in console):
ActiveRecord::Base::Attachment
=> Paperclip::Attachment
I can get access to my Attachment model inside AttachmentController and by default it console but nowhere else.
Also i tried to create simple object from console without attached file.
a.errors.sort
[]
a.save
TypeError: can't dump anonymous class Class
As you see object a has no errors but throws error in save.
Finally my aim is to copy a collection of Attachment objects from one holder-object to another. I mean deep copy, so files should be copied too. If you have any suggestions about these points I'll appreciate.
You can always access your class via "::Attachment", but make sure you're using the latest version of Paperclip. There were some namespace collision bugs that were fixed.
Related
I'm working some legacy code right now in seeds.rb. The previous developer used a method like this to add rows to the tables:
things_holder.oldthings_add(name)
where:
oldthings.rb
belongs_to :things_holder
things_holder.rb
has_many :oldthings
I can manipulate the object they created and adjust the seeds of the models they'd created. However, when I try to do the same to a model that I created myself (newthings), I can't seem to make it work.
Instead I get:
undefined method: newthings_add
where does this things_add method come from? I don't see it in any of the oldthings.rb files
*_add is not a standard Rails / Active Record method, so it's either defined somewhere in your application, or it's provided by some other gem.
As #jvillian's commented, you may be able to things_holder.method(:oldthings_add).source_location to learn where the method is defined.
If that doesn't work, you could try passing a blatantly invalid value to the method (e.g. things_holder.oldthings_add(true)), and see where the backtrace points.
In my models I have an AssessmentItem which has_many :items_levels. When I try to import an ItemLevel I get the error
Error during import: AssessmentItem(#70286054976500) expected, got
AssessmentItem(#70286114743280)
(/Users/stoebelj/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activerecord-4.2.7.1/lib/active_record/associations/association.rb:218:inraise_on_type_mismatch!')
As far as I know, the parent record exists and I am referencing it with the correct mapping key.
Can someone give me an idea of what this error means and what might be the culprit?
I's too having these problems.
Since, in development environment, each time you make changes to class and save it in any editor, the class is registered as a new class and hence the mismatch of class' object instances.
Stop rails server
Run it again
Reload your page
Now, try uploading again
Repeat each time you make any changes to your code.
So I have built an API using Ruby/Sinatra and MondoDB. I'm now writing an Admin panel using Rails that connects to the same database. One of our collections is named 'files' since we need to model an object of such kind in our API. Now, when I wanted to create the File model in the Admin Panel, I came across the fact that I can't create a class called 'File'. Doing so results in the error attached
Even 'File1' is an acceptable class name. How do I now create the File model in my Rails app? I'm using Rails 5.0.0.1.
I believe that is happening because File is a reserve Rails word. I would probably rename the model to something like FileResource .
List of Reserved words: http://www.rubymagic.org/posts/ruby-and-rails-reserved-words
The file is ruby's class, you can't and should't use the name in model. Or you will replace the original File .
FileResource is a good name!
I'm using Paperclip with Rails 4 to add attached video files to one of my models. I am able to name of the saved file after its new id like this:
has_attached_file :file, :url=>"/tmp/video_uploads/:id.:extension", :path=>":rails_root/tmp/video_uploads/:id.:extension"
This causes them to get saved to the right place, with the right name + original extension. However, when I look in the database, the file_file_name field for the new record is still the original file name (EX: scooby-dooby-doo.MOV). How do I fix this?
As far as I know, it's just an attribute:
object.file_file_name = 'something_else'
object.save
It seems to be there to retain the original file upload name. Changing that value doesn't really do anything.
edit: You say you're trying to make it easy to find the associated file, are you aware of the .url or .path methods on file?
object.file.path
object.file.url
Have a look at the attachment object on github.
In looking at that, it appears that re-assigning the value of file_file_name will "break" file.original_filename, in that it won't be accurate anymore. If you just want the file portion of the actual stored file, you could instead try something along these lines:
class MyModel < ActiveRecord::Base
has_attached_file :file
def actual_filename
File.basename(file.url)
end
end
I'm trying to create a custom filename for files uploaded via the paperclip gem using Paperclip.interpoles in the initializer. The problem I'm having is this is updating the custom filename on the file system when the file is uploaded, but the database filename remains the name of the source file. Is there a better way then having to reassign the database attribute to handle this?
Try using before_create callback in ActiveRecord. As paperclip will not write the attached resource to disk until ActiveRecord::Base#save is called, this seems to be the right time to create your custom filename.
To do so, just register an ordinary method to create the custom filename. This will change the name of the attached image, which you'll then find on your file system and in your database.
Let's say you have a model, where you want to attach an image with a custom random filename.
In your model:
has_attached_file :image
before_create :randomize_image_file_name
Also in your model:
def randomize_image_file_name
extension = File.extname(image_file_name).downcase
self.image.instance_write(:file_name, "#{ActiveSupport::SecureRandom.hex(8)}#{extension}")
end
You can declare your method anywhere you want, though it's considered good practice to declare callback methods as protected or private.
This will save the attachment with a custom randomized filename.
Hope this helps.