Belongs_To Presence in Rails 5 Not Working

Belongs_to presence in Rails 5 not working

According to the issue re weird behaviour of config belongs_to_required_by_default, it seems like one of your other gems intervenes in ActiveRecord::Base and causes the bug.

One of workarounds to the issue is to move the line

config.active_record.belongs_to_required_by_default = true

from initializers directly into application.rb.

This worked for me smoothly.

Rails 5 belongs_to default behavior

You are right, belongs_to is required by default in Rails 5. In your example, you are not running a Rails 5 app, you are just using ActiveRecord.

belongs_to is required by default in Rails 5 because when you generate a new Rails 5 app, you get a file config/initializers/new_framework_defaults.rb which has the following line:

# Require `belongs_to` associations by default. Previous versions had false.
Rails.application.config.active_record.belongs_to_required_by_default = true

To achieve similar results in your example, you have to set this config option for ActiveRecord as well (by default it's not set, which means it's nil, aka falsy value):

# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.belongs_to_required_by_default = true # <-- This line :)

Now if you run your tests, they will pass.

For more info, reference this pull request.

Rails belongs_to not validating id when optional

The best solution I could come up with is this:

class Quota < ActiveRecord::Base
belongs_to :domain, optional: true
belongs_to :project, optional: true

validate :validate_associations

def project_id=(val)
Project.find(val) unless val.nil?
super
end

def domain_id=(val)
Domain.find(val) unless val.nil?
super
end

private

def validate_associations
errors.add(:base, 'Specify a domain or a project, not both') if domain && project
errors.add(:base, 'Must specify a domain or a project') if domain.nil? && project.nil?
end
end

Thanks for helping iron things out @vane-trajkov. I found I really needed to do use the find method when setting the domain_id or project_id, because Rails was happy to set it to an invalid ID. Using project= and domain= work fine as-is since they pretty much ensure the ID has already been set to a valid value.

Rails associations has_many and belongs_to not working

You are almost there, just missed a small detail. Always follow what the interpreter / compiler says and you can find what went wrong.

In your case :clients should be singular in the accepts_nested_attributes_for since it is a belongs_to

class Document < ActiveRecord::Base
has_paper_trail

belongs_to :client

accepts_nested_attributes_for :client, allow_destroy: true, reject_if: :all_blank

validates :name, presence: true
end

Rails - Creating a belongs_to association between models prevents me from editing the model

Use:

belongs_to :moderator, class_name: "User", optional: true

In rails 5, belongs_to enforces existence of the associated record by default. You need to use optional: true in order to allow moderator_id to be nil.



Related Topics



Leave a reply



Submit