Configuration to allow optional belongs_to association not working in rail 6 - ruby-on-rails-6

In rails 5, I could make belongs_to association optional using this setting:
Rails.application.config.active_record.belongs_to_required_by_default = false
but this does not appear to work in Rails 6. Is there a way to do this in Rail 6?

Looking at the new framework defaults file for Rails 5, it had the following
# config/initializers/new_framework_defaults.rb
# Require `belongs_to` associations by default. Previous versions had false.
Rails.application.config.active_record.belongs_to_required_by_default = true
So it appears that the option has been removed completely for Rails 6. So you will need to do it on a case by case basis by adding optional: true. In my case, in most cases, I ended rewriting the code so that the association was required.

The option still works in Rails 6, but you need to make sure you set it after loading default config values, i.e.
config.load_defaults 6.0
The best place place would be towards the end of config/application.rb
...
# Don't check for the existence of belongs_to records
config.active_record.belongs_to_required_by_default = false
end
end

Related

2sxc update an entity to set IsPulished to false

I need to be able to set the IsPublished value of an existing entity to false through the WebAPI. According to https://2sxc.org/en/docs/feature/feature/3360 :
Just so you don't get things wrong: an Update with "IsPublished"=false will not hide the existing entity, but will hide the change you just made.
Is there a way to do this through the WebAPI, or can this only be done through the 2sxc GUI panel?
In v13.02 you can include IsPublished true or false. The behavior will be
true will set everything to published
false will set it to draft, and if there was a published one before, it will remain published
In v13.03 you will be able to do true, false and "draft"
true will set everything to published
false will set everything to unpublished
"draft" will set the updated data to draft, and if there was a published one before, that will leave.
It will be available in 2sxc 13.02
old answer:
As of now it can only be done with very internal APIs.
But I believe it would be fairly easy to add it to the simple data-api - the App.Data.Update(...) - so if you think it's important, just add an issue.

`foreign_type` relationship failing on Rails 6

I'm trying to understand this relationship setup on this model, and why is this an issue on Rails 6.
Coming from Rails 3, this relationship is defined as:
belongs_to :entity foreign_key: 'user_id', foreign_type: 'ruby_type'
This worked fine, all the way up to Rails 5 (At least our crawl and traceroute did not bring this as an issue).
However, once we got to Rails 6, we found the following:
ArgumentError: Unknown key: :foreign_type. Valid keys are: :class_name, :anonymous_class, :primary_key, :foreign_key, :dependent, :validate, :inverse_of, :strict_loading, :autosave, :required, :touch, :polymorphic, :counter_cache, :optional, :default
Was this syntax changed for Rails 6?
foreign_type option can only be applied together with polymorphic - so make sure this is a polymorphic association you are after. If the association is not polymorphic I would get rid of foreign_type.
belongs_to :entity, foreign_key: 'user_id', foreign_type: 'ruby_type', polymorphic: true

Mule ESB Kernel 4.2 - gap in wrapper.conf java.additional proeprties

I have noticed in wrapper.conf file, that there is a gap in java.additional. properties:
wrapper.java.additional.5=-Dorg.glassfish.grizzly.nio.transport.TCPNIOTransport.max-receive-buffer-size=1048576
wrapper.java.additional.6=-Dorg.glassfish.grizzly.nio.transport.TCPNIOTransport.max-send-buffer-size=1048576
# Limit the Metaspace Size to protect system memory from unwanted usage
# Increase this value if you get "Java.lang.OutOfMemoryError: Metaspace" error
wrapper.java.additional.8=-XX:MaxMetaspaceSize=256m
Is this OK? I think all additional properties starting from 8 are ignored. Also, when I add new property (18, because last active property in the original file is 17) - it is ignored as well.
Yes, gaps are ok because there is a configuration option in the default wrapper.conf set to accept the gaps:
# Ignore gaps in additional properties sequence
wrapper.ignore_sequence_gaps=TRUE
Ignored options are a symptom of some other problem. Are you running in Windows as a service? There is a known permissions issues about that where auto generated properties override manually set properties: https://help.mulesoft.com/s/article/wrapper-conf-Configuration-is-not-Updated-when-Mule-Runtime-is-running-as-a-Windows-Service
In that case you need to uninstall and reinstall the Mule service.

hibernate 5.4.2 UnwrapValidatedValue automatic

we upgraded from HV 4.x to HV 5.4.2 and now when we have interface like following
#NotNull
List<AccountInfo> getMultiClientAccountBalances(#NotNull ClientContext clientContext, #NotNull Optional<AccountFilter> accountFilter);
I'm getting error:
javax.validation.UnexpectedTypeException: HV000186: The constraint of type 'javax.validation.constraints.NotNull' defined on 'getMultiClientAccountBalances.arg1' has multiple matching constraint validators which is due to an additional value handler of type 'org.hibernate.validator.internal.engine.valuehandling.OptionalValueUnwrapper'. It is unclear which value needs validating. Clarify configuration via #UnwrapValidatedValue.
I know it can be fixed by adding #UnwrapValidatedValue to the field, but this must be added to every method what is a lot of work for me. Is there any simpler solution (besides upgrade to HV6.x)
Unfortunately I don't see how we could change this behavior in 5.4 without breaking other use cases.
And there is no easy way to disable the optional value handler as it's added unconditionally.
So I would say you have two solutions:
a search and replace of all #Constraint Optional to add the UnwrapValidatedValue option
or move to HV 6, where we totally reworked this feature and where, I think, it should work as you expect it. I know you didn't want this answer but it is what it is...
The issue with 1. is that we removed this annotation from HV (it was experimental) in favor of a standard feature included in Bean Validation so you will have to remove it when moving to 6.
I don't know your exact environment but HV 6 is highly compatible with the previous versions so it might work very well. Just be careful about the dependencies as we changed the groupId of the artifact from org.hibernate to org.hibernate.validator. Also be aware that you need to update the validation-api from 1.1 to 2.0.
6 is already very stable and if you have any issues with it, we will fix them right away.

Rails Active Model Serializer Include Concerns

Is it possible to include code in a serializer? I have some commonly used methods that I'd like to just include instead of always repeating them.
If you are looking for some code as an example:
1) Create your specialized serializer that will include the specialized concern
# app/serializers/specialized_serializer.rb
class SpecializedSerializer < DefaultSerializer
include SpecializedConcern
...
end
2) Create your specialized concern in a new concern folder
# app/serializers/concerns/specialized_concern.rb
module SpecializedConcern
extend ActiveSupport::Concern
included do
include SomeModule
...
end
def some_method
...
end
end
3) Add your new serialized concerns folder to your applications autoloaded path
# config/application.rb
...
config.autoload_paths += "#{config.root}/app/serializers/concerns"
...
Absolutely, you can. Either use concern, ApplicationSerializer, or compose another classes for sharing behavior should be okay.

Resources