I am trying to upgrade my rails 3.0.5 application with ruby 2.3.4. Originally it was ruby 1.9.3. I was able to fix most things by updating the gems. However, i m stuck on this one problem where when creating new active record objects, the time does not convert properly.
For example
Product.new(:bought_on => Date.today) will save the object with bought_on to be the date, not datetime.
I was able to narrow down the problem to the file
activerecord-3.0.20/lib/active_record/attribute_methods/time_zone_conversion.rb
For some reason its not calling these two functions, define_method_attribute and define_method_attribute=.
Any ideas?
I found the issue, the define_method_attribute under time_zone_conversion.rb is a protected method, and in ruby 2, the respond_to function always returns false for protected methods. Had to monkey patch to remove the protected attribute.
Related
I've recently done an rails and ruby upgrade, we don't have strong params in the app (I know it's legacy).
So the way it's done in the app we have the following
def all_params_permitted(this_params = nil)
this_params = params if this_params == nil
this_params.permit!
this_params.each do |i, v|
if v.kind_in?([Hash, ActionController::Parameters])
all_params_permitted(v)
end
end
end
Which loops through all params and just accepts everything, all_params_permitted is called throughout the app I would love to add strong params but that's a no-go for now.
The issue in the above method is kind_in? the upgrade I did for this app was rails 5.0.3 to rails 6.1+ and went from ruby 2.2.6 to ruby 3.0.1 so I'm not sure why kind_in? has stopped working. This is an old app (built-in rails 2) so not sure if this has been deprecated.
Any help here would be great.
Edit
I have tried kind_of? but no dice.
the upgrade I did for this app was rails 5.0.3 to rails 6.1+ and went from ruby 2.2.6 to ruby 3.0.1
This is asking for trouble. It is strongly advised to try upgrading one minor version at a time (e.g. rails 5.0 --> 5.1 --> 5.2 --> 6.0 --> 6.1), otherwise you're very likely to break things with little information on why it's stopped working/how to fix it.
Likewise for ruby versions... At an absolute minimum I'd postpone the final upgrade to ruby v3 until your application works fine under ruby 2.7.
I'm not sure why kind_in? has stopped working
Nor am I, because that's a custom method. You haven't show us how it's defined, and nor have you shown us the error message, so it's impossible for me to say with confidence what's gone wrong.
My guess is that it's implemented something like this:
class Object
def kind_in?(classes)
classes.any? { |c| self.kind_of?(c) }
end
end
i.e. it's a little wrapper around the built-in kind_of? method.
And with that said, I still have no idea why this would have "stopped working" due to a ruby and/or rails upgrade.
Not sure about kind_in?, also didn't find any reference to that method, also as you have not posted the error so not sure about your issue. is_a?, kind_of?, instance_of? are few methods that check the object class but they check only a single class. Looking at your code one option for your condition could be:
if [Hash, ActionController::Parameters].include?(v.class)
which will check if it belongs to one of these classes.
I tried upgrading my rails app from rails 4.1.0 => 4.2.7 but found that it broke my JSON views. In particular, ActiveRecord objects no longer render correctly.
For instance: Model.first.to_json in rails console now produces "#\u003cModel:0x007f9208c904e0\u003e", rather than valid JSON as before. Model.first.as_json works correctly, but for some reason, it seems to_json is calling to_s instead.
I tried overriding the ActiveSupport encoder but it didn't help, and am pretty stumped.
Any ideas?
(UPDATE: I also noticed Time.now.utc.to_json changed from 2017-12-07T19:38:47Z => 2017-12-07 19:37:55 UTC, the latter being == time.as_json and the former == time.to_s)
Rails 4.2.3, IdentityCache gem 0.2.5
I am calling ZipCodeLookup.fetch_by_id(value) and I am getting the following error:
undefined method `fetch_value' for #<Hash:0x007fbc6d261e08>
I have the following attributes in my ZipCodeLookup model
include IdentityCache
cache_index :zip_code, unique: true
I haven't seen anyone else with this issue. Does anyone know how to fix this?
fyi works in Rails 4.1.x
the error came from not clearing the cache. so run the following command in the rails console: IdentityCache.cache.clear
I've seen the same exception before without using IdentityCache. In my case, some ActiveRecord models were manually 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.
I am apparently having a huge problem switching from the plugin version of Paperclip to the gem version in my app. It's been my impression that there should be no difference whatsoever between a plugin and a gem of a specified version. However, I'm not seeing this as an easy transition at all.
Rails 2.3.11, Ruby 1.8.7
The plugin version I am using is version 2.3.3 and was upgraded on August 2, 2010. Attempting to update this to the gem of the same version basically killed all my tests, not being able to load a factory model which did not have its attachment loaded. It appeared that validate_attachment_content_type was also attempting to validate the attachment presence, and couldn't find it, so everything just started breaking. Again, with the plugin there are no problems and I haven't had any problems in all this time we've been using it. On the other hand, this problem seems to not occur past version 2.3.4. That's a whole other set of problems.
Basically, in all versions from 2.3.4 and up I get the problem below:
can't convert nil into String
/home/joshua/.rvm/gems/ruby-1.8.7-p334#paperclip_upgrade/gems/paperclip-2.3.15/lib/paperclip/storage/s3.rb:163:in `extname'
/home/joshua/.rvm/gems/ruby-1.8.7-p334#paperclip_upgrade/gems/paperclip-2.3.15/lib/paperclip/storage/s3.rb:163:in `to_file'
/home/joshua/.rvm/gems/ruby-1.8.7-p334#paperclip_upgrade/gems/paperclip-2.3.15/lib/paperclip/attachment.rb:94:in `assign'
/home/joshua/.rvm/gems/ruby-1.8.7-p334#paperclip_upgrade/gems/paperclip-2.3.15/lib/paperclip.rb:279:in `avatar='
/home/joshua/railscamp/app/app/models/organization.rb:311:in `copy_membership'
in all my tests that access my organization model.
The apparent offending code in this case is attempting to clone a membership model from one organization to another, with the * line being the offending call.
def copy_membership(membership)
m = membership.clone
u = m.user.clone
u.organization = self
m.organization = self
begin
m.avatar = membership.avatar *
rescue RuntimeError
m.avatar = nil
end
m.user = u
m.save
m
end
Does this make any sense to anyone? Why would the plugin work, but the gem of the same version just wrecks everything?
Update: I also don't appear to have any paperclip rake tasks available. Any ideas?
As it turns out, we should have been checking whether the filename is valid or not, rather than depending on a generic runtime error for detecting avatar presence.
In Rail 2.3.2 can I have merb style action definitions:
Eg: instead of
def show
#user = User.find(params[:id])
end
Can I have:
def show(id)
#user = User.find(id)
end
What kind of crazy monkey patching do I need to do to get this working, note I only need this working for MRI so ParseTree is an option.
Note: there is a Rails 3 port of this functionality now.
You could change all params in to instance var's (eg. #id) in a before_filter...
EDIT: I was wrong: Method#parameters has been added to Ruby 1.9.2.
Original:
No, this is not possible in Rails.
There is almost zero chance of this making it into Rails 3.0. merb-action-args used ParseTree which doesn't and will not work on Ruby 1.9, therefore making it unlikely that it will be included in Rails.
I am running Rails 2.3.3 and User.find(id) works just fine.