acts-as-taggable-on icon indicating copy to clipboard operation
acts-as-taggable-on copied to clipboard

ActiveModel::UnknownAttributeError with upsert

Open dewey opened this issue 1 year ago • 0 comments

Hey—

I'm using this library and I'm running into a problem where I'm not sure if it's something I'm doing wrong or a bug when using it with upsert_all. I'm using the following version in a Rails 7.0.4.2 project with Ruby 3.0.3.

Setup

    acts-as-taggable-on (9.0.1)
      activerecord (>= 6.0, < 7.1)

My model: Bookmark

  acts_as_ordered_taggable
  acts_as_taggable_on :tags

  def public?
    self.tag_list.include?('some-tag')
  end

Stacktrace

ActiveModel::UnknownAttributeError (unknown attribute 'tag_list' for Bookmark.):
19:45:33 web.1    | /Users/philipp/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/activerecord-7.0.4.2/lib/active_record/insert_all.rb:264:in `extract_types_from_columns_on'
19:45:33 web.1    | /Users/philipp/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/activerecord-7.0.4.2/lib/active_record/insert_all.rb:199:in `values_list'
19:45:33 web.1    | /Users/philipp/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/activerecord-7.0.4.2/lib/active_record/connection_adapters/postgresql_adapter.rb:515:in `build_insert_sql'
19:45:33 web.1    | /Users/philipp/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/activerecord-7.0.4.2/lib/active_record/insert_all.rb:153:in `to_sql'
19:45:33 web.1    | /Users/philipp/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/activerecord-7.0.4.2/lib/active_record/insert_all.rb:41:in `execute'
19:45:33 web.1    | /Users/philipp/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/activerecord-7.0.4.2/lib/active_record/persistence.rb:333:in `upsert_all'

The line inserted_bookmarks is the one causing the issues in this case.

                bookmarks = []
                tweet_upserted.each do |existing_tweet|
                  @bookmark = Bookmark.new
                  @bookmark.user = @user
                  @bookmark.tweet_id = existing_tweet['id'].to_i
                  @bookmark.source = 'FetchTwitterUserBookmarksJob'
                  bookmarks << @bookmark.as_json(except: %i[id created_at updated_at sent_at materialized_at note])
                end

                inserted_bookmarks = Bookmark.upsert_all(bookmarks,
                                                         unique_by: %i[user_id tweet_id],
                                                         returning: %i[id]
                )

Are you aware of anything in this library that would cause issues when used like that?

Thank you!

Update

I found that by adding tag_list to the except list of @bookmark.as_json I can prevent this error, I'm leaving this open as I'm not sure if that's something that should be done on the library level or that I'm just using it wrong.

dewey avatar Feb 21 '23 19:02 dewey