Rails 4 : undefined method `increment!' - ruby-on-rails

I am trying to fix a Redmine plugin written for Redmine 2.5 to work with Redmine 3 which uses Rails 4. When it executes the following code:
project_issue_key.increment!(:last_issue_number)
I get the following exception:
NoMethodError (undefined method `increment!' for #<ProjectIssueKey::ActiveRecord_Relation:0x007f65f52cf648>):
I am thinking that the increment! method has been deprecated. How is this done in Rails 4?

You should call increment! on the object, not ActiveRecord::Relation. Something like ProjectIssueKey.first.increment!(:last_issue_number).
increment! is still present in Rails 4 , check these docs.

Related

ActiveRecord::Relation.concat failing in Rails 5

Stupid question but I'm not sure why this would work in Rails 4.2 but not in Rails 5.2.
FamilyCharacteristic.where(family_id: #user.family_ids)
.concat(#user.characteristics)
Specs fail in 5.2:
Failure/Error:
FamilyCharacteristic.where(family_id: #user.family_ids)
.concat(#user.characteristics)
NoMethodError:
undefined method `concat' for #<ActiveRecord::Relation []>
Did you mean? count
Was concat removed from ActiveRecord::Relation in 5.2 or was FamilyCharacteristic.where(family_id: #user.family_ids) somehow a different object in < 4.2?
Thanks for any help.
I did some digging and found out that:
FamilyCharacteristic.where(family_id: #user.family_ids)'s class didn't change, it's still ActiveRecord::Relation
Relation didn't and still doesn't define its own concat method, but it was delegated to Array#concat until this commit happened, so in Rails 4.2 SomeModel.where(id: ids).concat(some_records)(which returns an Array) was actually the same as SomeModel.where(id: ids).to_a.concat(some_models)
After mentioned before change in ActiveRecord::Delegation, in Rails 5.2, the only methods delegated to Array are the ones specified in this module and concat is not among them
To sum up - concat from your example was never part of ActiveRecord but was delegated to Array#concat and that's why it worked. It's no longer delegated in Rails 5 so it throws NoMethodError.

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)

Rails 3 to Rails 4 upgrade undefined method to_input_field_tag

I'm in the process of upgrading a Rails 3 application to Rails 4 and I get the error message undefined method to_input_field_tag for class ActionView::Helpers::InstanceTag NameError when trying to start the Rails server.
Looking at API Dock http://apidock.com/rails/ActionView/Helpers/InstanceTag/to_input_field_tag it shows this method as deprecated or moved.
Has this been moved or what is an equivalent replacement for this in Rails 4?
The method has been deprecated and it appears that
ActionView::Helpers::InstanceTag
has been refactored into:
ActionView::Helpers::ActiveModelInstanceTag
Comparing the source code between Rails 3 and 4 it appears that the way input fields are rendered has changed:
# ~/rails-3-2-stable/actionpack/lib/action_view/helpers/form_helper.rb
def text_field(object_name, method, options = {})
InstanceTag.new(object_name, method, self, options.delete(:object)).to_input_field_tag("text", options)
end
Changed to:
# ~/rails-4-2-stable/actionview/lib/action_view/helpers/form_helper.rb
def text_field(object_name, method, options = {})
Tags::TextField.new(object_name, method, self, options).render
end
It appears that many gems were affected by this change in the process of upgrading to Rails 4. So if this error is coming from a gem, check to see if a Rails 4 version is available.
If this code is something you wrote, then you can attempt to use the new Tags class depending on what exactly you're trying to accomplish.

Squeel working in Rails Console but not in Controller. Error: undefined method `method' for nil:NilClass

I'm using Squeel, and I have an odd problem. The following line runs just fine in Rails Console, but gives an error when run in the model's controller!
Issue.joins{property.users}.where{property.users.id == 1}
Issue belongs_to Property, and Property has_and_belongs_to_many Users. I'm trying to load all the issues associated with the user_id == 1.
I get the error: "undefined method `users' for nil:NilClass" when I run the line in the IssuesController, but it works in Rails Console. What's wrong?
P.S. I'm running Rails 4.0.3, Ruby 2.0.0, and Squeel 1.1.1
Try this
my_properties = Property.where{user_id == 1}.select{issue_id}
Issue.where{id.in(my_properties)}

.to_date method failing

I am using rails 3.2.16 and ruby 1.9.3
I get this error from the localhost browser after rails s.
undefined method `to_date' for nil:NilClass
like this
NoMethodError in Products#new
Showing /Users/main/railscasts-episodes/episode-350/store-
after/app/views/products/_form.html.erb where line #27 raised:
undefined method `to_date' for nil:NilClass
in irb> mode it works only when I
require 'date'
Now my question is about my rails project. In which file in my project I should add
require 'date'
I added it into my model it did not work. Or, maybe I need to install any specific gem? to make this work
The problem you have isn't an issue with to_date, it's a problem with the object you're trying to perform the method on
Your issue will either be:
You have not declared the variable / object
Your variable / object is not populated with any data
I'd highly recommend posting the controller action you're using to invoke the to_date method. Obviously, there may be an issue with the way you're calling it, but the immediate issue is you're trying to deal with an unpopulated variable

Resources