Skip to content

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

Open
@marckohlbrugge

Description

# 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?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions