How to download ActiveStorage attachments to tmpdir - ruby-on-rails

The objective is to download an attached file to tempdir for subsequent usage. The documentation says to use ActiveStorage::Blob#open which seems simple enough.
I'm getting errors so please explain what I'm doing wrong:
Calling #flower.photo.open results in NoMethodError (private method 'open' called for #<ActiveStorage::Attached::One:0x00007f9780625100>)
Calling #flower.photo.blob.open` results in NoMethodError (private method 'open' called for #<ActiveStorage::Blob:0x00007f9780615c50>)
Examining the source code I'm not sure why I'm getting the private method error.

That method isn't released until rails 6 next year.
There’s a similar SO question here with more info and a recommendation.

Related

Ruby on Rails: Fetch_values Rollback error

I have upgraded my Ruby to 2.5 and after rectifying many dependency issues, I'm stuck at a place.
There is save method being called which saves the records, but somehow it do not works now and shows following error:
500 Internal Error
undefined method 'fetch_value' for #<Hash:0*0007e589e>
Did you mean fetch_values
each_value:
Earlier the same .save was working perfectly fine.
I've seen the same exception before. In my case, some ActiveRecord models were marshaled via Marshal::dump and saved as binary stream. Then, Ruby and Rails were upgraded.
Afterwards, calling Marshal::load on the marshaled copies would retrieve them and object.class would show the right model's name, but accessing any attribute within would throw the same exception.
I had to clear the marshaled copies and generate new ones.

Ruby on Rails: NoMethodError undefined method `get_yaml' for String

I tried to deploy a non public project but sometimes when tried to use
report pages (based on prawn) I have this problem:
NoMethodError (undefined method `get_yaml' for #<String:0x7fdecffd3738>):
The piece of code is:
report_content = report.draw(report_content.get_yaml)
Someone know: where are get_yaml come from?
Thanks.
The method #get_yaml is not a part of of the standard Ruby library, although #to_yaml is defined if the yaml library has been loaded. #get_yaml, if it exists, is defined by the application or by some library that the application is loading.

How to extend the String class in a command line app?

I primarily work in Rails and I'm using a command line data conversion gem, "Mongify" and I am stumped about how to extend core classes in a Ruby cli app.
I want to extend the String class with an .is_date? method to check whether a string can be converted to a Date. I've got it working in the Rails Console,
I added a string.rb file to lib/ext with the following;
class String
def is_date?
begin
return true if Date.parse(self)
rescue
#do nothing
end
return false
end
end
Then in a Rails console I do a require 'ext/string' and it will work.
But I can't figure out how to get it to work in the Mongify cli app. I copied string.rb into the lib folder of the gem and I've tried adding require 'string' to a number of different files in the gem, but I keep getting undefined method errors.
Can someone point me in the right direction?
How about you require it from lib/mongify.rb like so:
require 'string/extensions.rb'
And then put your code in lib/string/extensions.rb
Let us know the exact undefined method errors you're getting in case this isn't the solution.
To help you with the debugging exercise that would give you the answer you need. Start by putting a breakpoint right before the place of the function call.
In the debugger, load the required document and then step past your breakpoint to the next one after the call has occurred.
Once you have this working, then start earlier in the stack trace – in a file that loaded before that one. Keep moving backwards until you get to a sufficiently early part in the load process of the gem, and make that be the place you load your code.

undefined method `delay' for UserMailer:Class

I am getting the following error while writing mail notification process asynchronously by using delay method.
NoMethodError in SampleController#create
undefined method `delay' for UserMailer:Class
I have the following code in my controller.
UserMailer.delay.idea_author_notification(self,nfication)
I have already installed delayed_job gem also started delayed_job by using jobs:work rake task.
Shall I need to do some other changes for using delay method for executing mail related code in background ?
Please help me on this asap..
Thanks in advanced...
the method delay is not existed for any action mailer class. So you can not call that method for the UserMailer:Class.
For that you can check all the methods for the mailer class in the rails console. i.e
UserMailer.methods
and to check particular method existed or not, run the following line.
UserMailer.methods.include?(:delay)
The above line returns false
OR
if you want to use delayed jobs, please go through the following link in github. It would definitely help you.
delayed_jobs_for_rails2.x.x

What are reasons "reload!" doesn't always seem to work in the Rails console?

I'm just getting the hang of Rails console, and finding it useful for quickly testing methods in my classes. I know that I can make changes to my Models, then
> reload!
to grab those updates, but sometimes I'll find that it doesn't seem to reload my latest code. Does Rails cache code somewhere?
In a really simple pseudo example, I may have bad code on line 100:
100: u = User.alll
and in the Rails console, when I run this method, I might get an error similar to:
NoMethodError: undefined method `alll' for User:Class ... on line 100
then modify my code, fixing the error
100: u = User.all
then reload:
> reload!
and then, when calling the method in this class that has the correct code, it still will say
NoMethodError: undefined method `alll' for User:Class ... on line 100
When clearly, the error is fixed, and the offending line isn't even on line 100 anymore. Is there a way to force/hard-reset the "reload!" command?
My guess would be that you're doing something like:
Create an instance of User
Call someMethod on the instance
You get an error, and you go and fix it
reload!
You call someMethod on the existing instance and get the error again
So you're calling the method on an instance that hasn't itself been reloaded. Its class has been reloaded, but the instance is already in memory - with bugs and all.
That would be my guess at least (not 100% sure).
Point is, if you create a new instance after the reload! and call your method on that new instance, it should stop complaining.

Resources