Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails association Errors #82

Closed
omerpucit opened this issue Jul 18, 2022 · 3 comments
Closed

Rails association Errors #82

omerpucit opened this issue Jul 18, 2022 · 3 comments

Comments

@omerpucit
Copy link

omerpucit commented Jul 18, 2022

Expected Behavior

Should working with #<ActiveModel::Errors [#ActiveModel::NestedError

Actual Behavior

Giving error related to relational data errors.

Like

class Book < ApplicationRecord
  belongs_to :author, autosave: true

  delegate :author_name, :author_name=, to: :lazy_load_author

  def lazy_load_author
    author || build_author
  end
end

# Let's go with author_name, instead name
class Author < ApplicationRecord
  has_many :books
  validates :author_name, presence: true
end

book = Book.new(book_name: 'ABC', author_name: '').save
render jsonapi_errors: book.errors, status: :unprocessable_entity

This one giving error
NoMethodError - undefined method `book.author_name' for #<Book

Steps to Reproduce the Problem

Specifications

  • Version: Latest
  • Ruby version: Latest
@stas
Copy link
Owner

stas commented Jul 19, 2022

@omerpucit thanks for the report, but I don't see how this is related to jsonapi.rb.

In fact, it's pretty clear that the error is somewhere within the way you set up things:

NoMethodError - undefined method `book.author_name' for #<Book

If it helps, I'm happy to review the serializer for the Book, but there's little else I can do to help here.

I'll be closing this, but feel free to follow-up if you find anything I did miss.

@stas stas closed this as completed Jul 19, 2022
@omerpucit
Copy link
Author

omerpucit commented Jul 19, 2022

The issue was with the nested objects ( and with the rails method -> errors_object.generate_message ). Anyway, I have already fixed it for my own case.

attribute :detail do |object, params|
      error_key, error_hash = object
      errors_object = params[:model].errors
      # Rails 4 provides just the message.
      if false && error_hash[:error].present? && error_hash[:error].is_a?(Hash)
        message = errors_object.generate_message(
          error_key, nil, error_hash[:error]
        )
      elsif false && error_hash[:error].present?
        message = errors_object.generate_message(
          error_key, nil, error_hash
        )
      else
        message = error_hash[:message]
      end

      errors_object.full_message(error_key, message)
    end

@stas
Copy link
Owner

stas commented Jul 19, 2022

@omerpucit we do not support, nor plan to support nested resources/errors. And I generally do not recommend this pattern.

Glad you worked it out 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants