Multiple file upload carrierwave mongoid - ruby-on-rails

I have been having issues with implementing multiple file upload with the carrierwave-mongoid gem. Multiple file upload with carrierwave is well documented for ActiveRecord but not Mongoid. I have tried to mirror the ActiveRecord tutorials are much as possible but cannot seem to get it right. My uploader is mounted as follows.
class Product
include Mongoid::Document
.....
attr_accessor :product_images, :product_images_cache
mount_uploaders :product_images, ProductImageUploader
end
The main issue that I am having is with saving the uploader instance. In my controller with a pry in the create block, I am able to create a Product instance that contains the uploader instances, but when I try to retrieve the Uploader instance via the rails console the array is empty. see below.
[22] pry(#<ProductsController>)> #product
=> #<Product _id: 58726dae75e505a9ddc43f20, name: "Post", description: "This is a post\r\n", rating: nil, category_id: 1, seller_id: 1, approved: false, approved_by: nil>
[23] pry(#<ProductsController>)> #product.product_images_urls
=> ["/uploads/tmp/1483894190-43485-0005-7191/download.jpeg", "/uploads/tmp/1483894190-43485-0006-6662/TRACK.jpg"]
[24] pry(#<ProductsController>)> Product.last
=> #<Product _id: 58726dae75e505a9ddc43f20, name: "Post", description: "This is a post\r\n", rating: nil, category_id: 1, seller_id: 1, approved: false, approved_by: nil>
[25] pry(#<ProductsController>)> Product.last.product_images_urls
=> []
What is notice here is that the images are not being save in the uploads directory. I have spent quite a bit of time researching this but no leads. Any help will be greatly appreciated.
Update: This method is currently not supported by CarrierWave-Mongoid. My initial thoughts where right the Uploader object was not being created/saved https://github.com/carrierwaveuploader/carrierwave-mongoid/issues/155. I am currently looking for a workaround for this - any pointer in this direction will be great

Related

Paperclip and Zip - My .zip file is magically transforming into a .png

I'm having a problem with Paperclip. Easily enough, I am trying to get a .zip folder uploading using Paperclip - I've read around and it seems like it can be done.
However, my .zip uploads transform into the default 'missing.png' file - and I have no earthly idea why. The file is, er... well, the process is going through and I am not getting any errors, but obviously something is not turning out right.
I've followed the instructions of Can't upload zip files using ruby on rails and paperclip gem but it's still not... changing anything.
From what I can tell through a rails console run of the model in question, the file isn't uploading at all. The problem is, I'm not sure where or how or why.
Here's my book model
book.rb
has_attached_file :content
validates_attachment_content_type :content, :content_type => ["application/zip, application/x-zip"]
before_post_process :skip_for_zip
def skip_for_zip
! %w(application/zip application/x-zip).include?(attachment_content_type)
end
end
And a quick rails console peek
2.1.6 :001 > Book.all
Book Load (0.7ms) SELECT "books".* FROM "books"
=> #<ActiveRecord::Relation [#<Book id: 3, title: "Test", summary: "La la la", rating: nil, author_id: nil, word_count: nil, created_at: "2016-10-28 03:53:22", updated_at: "2016-10-28 03:53:22", cover_file_name: nil, cover_content_type: nil, cover_file_size: nil, cover_updated_at: nil, content_file_name: nil, content_content_type: nil, content_file_size: nil, content_updated_at: nil>, #<Book id: 4, title: "ugh", summary: "", rating: nil, author_id: nil, word_count: nil, created_at: "2016-10-28 03:59:33", updated_at: "2016-10-28 03:59:33", cover_file_name: nil, cover_content_type: nil, cover_file_size: nil, cover_updated_at: nil, content_file_name: nil, content_content_type: nil, content_file_size: nil, content_updated_at: nil>]>
2.1.6 :002 >
Ignore the 'cover' section, there was a separate Paperclip attachment that I hid to diagnose the 'content' problem.
But note, I'm not getting ANY errors. At all. Whatsoever. So. Any help from the StackOverflow Rails Gawds would be greatly appreciated for a newb like me.
Solved!
As #henners66 suggested, I was completely missing the :content as a permitted param on my books controller - further, turns out I was declaring the 'valid content types' incorrectly. It should have been:
book.rb
has_attached_file :content
validates_attachment_content_type :content, :content_type => ["application/zip", "application/x-zip"]
before_post_process :skip_for_zip
def skip_for_zip
! %w(application/zip application/x-zip).include?(attachment_content_type)
end
end
Thanks everyone!

Mongoid: Caching Eager Loaded Documents

We have this code:
class Band
include Mongoid::Document
has_many :albums
end
class Album
include Mongoid::Document
belongs_to :band
end
#bands = Band.includes(:albums).entries
This is great because now I can run #bands.first.albums without hitting the DB.
But now, if we write this includes to the rails cache...
Rails.cache.write('bands', #bands)
...we then read the cache.
bands = Rails.cache.read('bands')
This returns an array of band documents...
[#<Band _id: 536a53c969702d208f240000, created_at: 2014-05-07 15:39:53 UTC, updated_at: 2014-05-08 15:55:29 UTC, name: "Pink Floyd", fan_count: 394857, #<Band _id: 536adf2a69702d1574130000, created_at: 2014-05-08 01:34:34 UTC, updated_at: 2014-05-08 01:35:40 UTC, name: "Tool", fan_count: 2958394, #<Band _id: 536bcad169702d743e1e0000, created_at: 2014-05-08 18:20:01 UTC, updated_at: 2014-05-08 18:27:10 UTC, name: "My Morning Jacket", fan_count: 3945734]
...and then we can't get the albums.
bands.first.albums
NoMethodError: undefined method 'albums' for #<Array:0x00000104df50c0>
Is there a special way to cache these eager loaded documents with Rails or Mongoid?
FYI we're using Mongoid 4.
That looks like to be a eager load issue on Mongoid.. Can you go to https://github.com/mongoid/mongoid/issues/new and open a new issue, so we can work on a fix for it, before releasing the final 4.0.0 version.
thanks

factory_girl does not set model attribute

factory_girl is not setting the url attribute on the model correctly. I looked to see if there are reserved words, but I found nothing.
factory :attachment do
association :attachable, factory: :upload
url "some/path"
description "Some important file"
end
I get this failure. Notice url: nil
1) Attachment should be valid
Failure/Error: it { should be_valid }
expected #<Attachment id: 1048, attachable_id: 1047, attachable_type: "Upload", name: nil, url: nil, created_at: nil, updated_at: nil, description: "Some important file"> to be valid, but got errors: Url can't be empty
# ./spec/models/attachment_spec.rb:14:in `block (2 levels) in <top (required)>'
I ran factory_girl in the console and got the same result with FactorGirl.attributes_for :attachment
Running Rails 4 with FactoryGirl 4.2.1.
I was having this same issue and after hours of banging my head on the desk I decided to stop and restart my Spring server. It started working like magic.
Before restarting my Spring server I was able to set attributes using create :object, new_field: true but just using create :object wouldn't work, even if I had new_field true in my factories.rb file.
Late to the party but I had the same problem. I had added all my attributes after the initial migration, and it turned out I was using attr_accessor instead of attr_accessible in my model.

Strange problem with activerecord fetch/find with column name 'changes' in a RAILS 2.3.8 model

How is this possible?
Loading development environment (Rails 2.3.8)
>> wq = Wq.first(:conditions =>['widget_id=? AND qs_id=?',1,1])
=> #<Wq id: 1, widget_id: 1, qs_id: 1, operator: 0, requirements: "2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2", changes: "1", route: 2, created_at: "2010-09-07 08:11:05", updated_at: "2010-11-24 10:25:53", body: "Which specific area of gyt are you aiming to addres...", options: "['xyz','pqr']", input_type: nil, status: 1>
>> wq.changes
=> {}
>> wq.changes
=> {}
>> wq.requirements
=> "2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2"
>> wq.changes
=> "1"
Why is wq.changes coming as null initially and then after logging wq.requirements, wq.changes seems to come fine?
All necessary fields that are being fetched are withing a attr_accessible in the model.
I am not able to understand this situation, please help all you rails gurus.
The attribute name 'changes' conflicts with the AR::Dirty functionality. You should probably pick a different name for that column.
Here's the rails3 api docs for Dirty:
http://api.rubyonrails.org/classes/ActiveModel/Dirty.html
In rails2 it's in ActiveRecord rather than ActiveModel.
If you aren't able to rename the column, you could work around the issue by calling #model_obj[:changes] instead.
https://github.com/rails/rails/blob/master/activerecord/lib/active_record/base.rb#L1466

Error with "to_sql" on Rails 3 Beta 4

I'm testing Rails 3 beta 4 on Ruby 1.9.2-head, and when I start a
console and do:
Game.first.to_sql
I get this error:
ArgumentError: wrong number of arguments (0 for 1)
I know it can find the Game record, because when I type:
Game.first
it returns:
=> #<Game id: 1, name: "Galaga", created_at: "2010-06-19 11:02:37",
updated_at: "2010-06-19 11:02:37">
What am I missing? I just want to make the to_sql work in a very simple
case.
.
When you run Game.first you are returning a Game object, not a ActiveRecord::Relation object as you are expecting.
To do what you're trying to do, you'll need to do:
Game.limit(1).to_sql
This lets you run it without to_sql and return the object as you expected it, although it will be in an array, which then you can run .first on it like you wanted anyways.
irb(main):004:0> Game.limit(1).to_sql
=> "SELECT `games`.* FROM `games` LIMIT 1"
irb(main):005:0> Game.limit(1).class
=> ActiveRecord::Relation
irb(main):006:0> Game.limit(1)
=> [#<Game id: 1, name: "Galaga", created_at: "2010-06-19 11:02:37", updated_at: "2010-06-19 11:02:37">]
irb(main):007:0> Game.limit(1).first
=> #<Game id: 1, name: "Galaga", created_at: "2010-06-19 11:02:37", updated_at: "2010-06-19 11:02:37">
When you dig into the source, when you run .first on an ActiveRecord::Relation it runs the following (which is the same as I showed you):
def find_first
if loaded?
#records.first
else
#first ||= limit(1).to_a[0]
end
end

Resources