I’m upgrading an app from Rails 4.2 to Rails 6. One of the models has this
class Document < ActiveRecord::Base
…
skip_callback :save, :before, :store_document!, if: :skip_processing?
When upgrading to 6, the “:store_document!” throws an error “”, which was not thrown in the older Rails version. “store_document” is not a method I have defined anywhere, so I’m assuming this is some kind of Rails shorthand for something. My question is what does the “store_xxx!” Do and how can I reproduce that in Rails 6?
this is the store accessor in Active Record
the usage of which is described here
Looks like the arguments for it changed in Rails 6 to
store_accessor(store_attribute, *keys, prefix: nil, suffix: nil)
so my bet is you have a
store :document
statement somewhere that needs to be updated to the proper syntax.
Related
I have a rails engine which encapsulates a piece of my application's funtionality. I have a bunch of models in the engine, which have various belongs_to associations defined. As of rails 5 these associations are supposed to be required by default, unless optional: true is specified in the definition.
I’m still able to create instances of the models without any validation errors. I haven’t specified optional: true on any of the associations, nor is the config optionconfig.active_record.belongs_to_required_by_default set anywhere. Besides, it was removed in rails 6 anyway.
I can't think of any reason the model instances would not fail validation. I would expect any instances of any model with an undefined belongs_to association would be invalid and raise an error. Why would these records pass validation?
I found my problem, thanks to #MatthiasWinkelmann for the tip. It turns out my engine was not calling load_defaults at all. I needed to add the following to spec/dummy/config/application.rb:
module Dummy
class Application < Rails::Application
config.load_defaults Rails::VERSION::STRING.to_f
... etc ....
end
end
here is an article containing more explanation:
An upgraded Rails gem does not upgrade your Rails configuration
I probably would have done better to mention in my question that I'm in the process of upgrading my application from Rails 4.2 to 6.1. The change was introduced in Rails 5.
I'm working on upgrading a Ruby 2.2.2 (Rails 4.1) app to Ruby 2.5.7 (Rails 5.2) and for a couple of models I'm getting some errors
From searching around, it sounds like there are some generic activerecord validation rules / messages? The messages are:
Status is invalid
User is invalid`
I am a novice at best with Ruby - so any suggestions on the best way to work through this error are appreciated!
In Rails 5, whenever a belongs_to association is defined, it is required to have the associated record present by default. That means, compared to Rails 4, each belongs_to :foo association basically adds internally a validate :foo, presence: true to the code too.
You have two choices:
Follow the new Ruby on Rails conventions and fix your tests by adding all required associated objects to the models.
Switch back to the old behavior for these kinds of associations by adding , optional: true to each belongs_to :foo line in your code.
There is actually the third option to switch off this behavior in the whole application, by adding a line like this to your application.rb
Rails.application.config.active_record.belongs_to_required_by_default = true
But that means your application will not follow Ruby on Rails conventions and defaults anymore and IMHO this ofter leads to problems with a later update.
Therefore my advice is: Fix your tests now and only make those associations optional that are really optional from the user's point of view – this might take a bit longer but causes certainly less trouble in the future.
I am upgrading old rails 2 app to rails 4. There are some code written in the model for creating like
create(activated: true, sita_code: MULTIPROPERTY.downcase,
access_list: MULTIPROPERTY.downcase)
I know we add permit parameters on controllers but I am getting this issue on model. How can I solve this
If you are working with Rails < 4.2, you might need to declare all the attributes under the corresponding model. So for about answer will be
attr_accessible :activated, :sita_code, :access_list
I've started to transfer an old application to a new Rails application. First, i've created it with Rails 5.0, taking the same database I was using in production.
One of my associations is like:
class Conta < ApplicationRecord
has_many :conta_cooperados, foreign_key: :id_conta
end
class ContaCooperado < ApplicationRecord
belongs_to :conta, foreign_key: :id_conta
end
If I make a query like:
Conta.first.conta_cooperados
I will have the informations I wanted, and it will generate the following SQL:
SELECT `conta_cooperado`.* FROM `conta_cooperado` WHERE `conta_cooperado`.`id_conta` = 1
As you can see, everything was working very well, but for some reasons I've decided to create a new application with Rails 5.1 and API option enabled.
And now, when I run the same query, the following SQL is generated:
SELECT `conta_cooperado`.* FROM `conta_cooperado` WHERE `conta_cooperado`.`conta_cooperado` = NULL
And this one doesn't bring the expected result, because the 'conta_cooperado' . 'conta_cooperado' = NULL part is wrong.
I don't know if this happens because I'm using Rails 5.1 instead 5.0, or if because I'm using the api mode.
UPDATE:
I just upgraded my Rails 5.0 application to 5.1 version, and, if until this upgrade everything was working, now I'm having the same problem. So, this situation happens only on 5.1 version of Rails. Is there a bug in this version?
A migration I created in a Rails 5 application had 5.0 passed into a method:
class CreateVariableKeys < ActiveRecord::Migration[5.0]
...
end
I would like to know what the [5.0] means.
It is a class method of ActiveRecord::Migration and is defined here.
It allows us to select the version of migrations we wish to use between 4.2 and 5.0. The method throws a:
"Unknown migration version ... "
error if an incompatible version is passed as an argument.
Production ready versions of ActiveRecord don’t have that method so it should go away as soon as Rails 5 goes out of beta.
This blog has more info too
It seems to be there so that you don't have to upgrade old migrations, when moving from rails 4 to rails 5. (There are some small changes in the migrations API).
In Ruby, you can define a method named [] on a class like so:
class Foo
def self.[](arg)
puts arg
end
end
And call it like this:
Foo["print me"]
--> "print me"
Foo[2.3]
--> 2.3
See this answer for an explanation.
In Rails 7.0, ActiveRecord::Migration[version_number] contains this code (source):
def self.[](version)
Compatibility.find(version)
end
Where Compatibility::find (source) finds the appropriate version of the migration, which you can verify using rails c:
irb(main):001:0> ActiveRecord::Migration[5.2]
=> ActiveRecord::Migration::Compatibility::V5_2
Hope that helps.