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

Breaks with Rails 5.1 #4

Open
nguyen-tran opened this issue Aug 29, 2018 · 3 comments
Open

Breaks with Rails 5.1 #4

nguyen-tran opened this issue Aug 29, 2018 · 3 comments
Assignees
Labels

Comments

@nguyen-tran
Copy link

Association between models not created when setting IDs and calling save. Dirty tracking will always return [nil, nil] even when setting IDs to the same IDs.

Gem: 0.2.1
Rails: 5.1

@uberllama
Copy link
Owner

The Dirty implementation in Rails 5.2 has changed to operate only against AR attributes. Changes are still propagated but dirty tracking is lost w 5.2. I will need to do a rethink/rewrite. Thx.

@uberllama uberllama self-assigned this Aug 31, 2018
@uberllama uberllama added the bug label Aug 31, 2018
@bazzel
Copy link

bazzel commented Sep 5, 2018

Not sure if this helps. I ran into the same issue when I wanted to track changes to an association.

project = Project.first
project.user_ids = [1,2,3]
project.changes

I added an extra column user_ids_cache to the projects table that gets updated by the after_ callbacks of the association. By aliasing some attributes you should get the same result as before.

class AddUserIdsCacheToPatch < ActiveRecord::Migration[5.2]
  def change
    add_column :projects, :user_ids_cache, :text
  end
end

class Project
  serialize :user_ids_cache, Array

  has_many :users, after_add: cache_user_ids,
                   after_remove: cache_user_ids

  alias_attribute :user_ids_changed, :user_ids_cache_changed
  alias_attribute :user_ids_was, :user_ids_cache_was
  
  private
  
  def cache_user_ids
    self.user_ids_cache = user_ids.sort
  end
  ...
end

@uberllama
Copy link
Owner

Thanks @bazzel just got back from vacation but I'll check out your solution.

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

No branches or pull requests

3 participants