paginate_all_by_something not workingi in rails 3 - ruby-on-rails

I'm upgrading my project to rails 3.0.1 , i have used paginate_all_by_something in the controller in rails 2.1.1 and rails 2.3.8 working fine but in rails 3.0.1 it displaying the undefined method `paginate_all_by_receiver_deleted' for #
error like this
If anybody faced like this problem kindly help me.

It might be because of backwards incompatibility of new version of will_paginate (~>3.0).
Read here for further details.

You can use paginate on collections instead like this:
User.find_all_by_receiver( receiver_id ).paginate
So it's up to you how you construct the collections.

Related

Rails method global params kind_in error when upgraded

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.

Upgrading ruby to 2.3.4 with rails 3.0.5

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.

Rails 4.1.0: undefined method `setup' for #<ActiveRecord::Validations::UniquenessValidator

I'm experiencing the following error when updating my Rails-app from 4.0.0 to 4.1.0 (Ruby 2.3.1):
undefined method `setup' for #<ActiveRecord::Validations::UniquenessValidator
The corresponding code is:
validator = ActiveRecord::Validations::UniquenessValidator.new({attributes: column, scope: scope})
validator.setup(self.class)
validator.validate(self)
This error seems to be known when upgrading to Rails 4.2. In Rails 4.1 the setup method should only be deprecated, as a result I'm really confused now.
Trying to replace "setup" with "initialize" as recommended in this first answer's comment does not work (as I'm on Rails 4.1).
I can reliably reproduce the error by up and downgrading between Rails 4.0.0 and Rails 4.1.0.
Any help appreciated!
As mentioned here, you can achieve this by passing class as an argument while initialising validator instance,
validator = ActiveRecord::Validations::UniquenessValidator.new({attributes: column, scope: scope, class: self.class})
validator.validate(self)

How to duplicate object "by value" in Rails?

I need to duplicate a product in my spree application. So
def my_duplicate_product(product)
product.dup.tap do |new_product|
new_product.slug = "#{product.slug}-#{rand(1000)}"
...
This code causes the original product#slug to be changed.
What am I supposed to do to get a copy of that particular product and leave the original unchanged?
Rails version: 4.0.3
Update:
The problem is not in Ruby, nor Rails — it's all about globalize gem (v. 4.0.0).
This error was fixed in 4.0.3.
It realy broke #dup so some values were "shared" between the original model and the duplicated one.
See GitHub issue tracker for more information: https://github.com/globalize/globalize/pull/352
Maybe clone works:
def my_duplicate_product(product)
product.clone.tap do |new_product|
new_product.slug = "#{product.slug}-#{rand(1000)}"
Clone method on ruby doc
Update globalize gem to 4.0.3
See GitHub issue tracker for more information: https://github.com/globalize/globalize/pull/352

Output the name of the view being rendered in rails 3.1

How does this translate to Rails 3.1?
#template.instance_variable_get(:#_first_render).name
It's supposed to output the name of the view being rendered. Note: it's not always the same as params[:action]
Thanks!
You can find one great answer for Rails 3.0.X here.
There is another for Rails 3.1.x but I tried it without success.

Resources