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

NoMethodError: undefined method `attached?' for <#Attachment> with nested records #186

Open
marckohlbrugge opened this issue Feb 23, 2023 · 0 comments

Comments

@marckohlbrugge
Copy link

# promotion.rb
class Promotion
  belongs_to :product, optional: true
  has_one_attached :image
  validates :image, processable_image: true
end
# product.rb
class Product
  has_many :attachments
end
# attachment.rb
class Attachment
  belongs_to :product
  has_one_attached :image
  # no validations
end

Running the following code returns an error:

product = Product.create! 
attachment = Attachment.create! product_id: product, 

promotion = Promotion.create! product: product
=> NoMethodError: undefined method `attached?' for #<Attachment …

Unfortunately the stack trace is useless as it leads to method_missing in active_model/attribute_methods.rb

There's two surprising things happening here:

  1. Somehow the validation inside promotion.rb ends up calling .attached? on Attachment which is just a regular ActiveRecord model. It should be calling it on the image instead.
  2. Removing that validation from promotion.rb resolves the issue.

Any idea what could be going on here?

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

1 participant