{"id":22289434,"url":"https://github.com/tf/cached_associated_attributes","last_synced_at":"2025-03-25T21:23:00.151Z","repository":{"id":455660,"uuid":"79332","full_name":"tf/cached_associated_attributes","owner":"tf","description":"Not maintained - Easily cache an attribute of an associated model","archived":false,"fork":false,"pushed_at":"2015-07-30T16:14:58.000Z","size":144,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-30T18:43:36.521Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tf.png","metadata":{"files":{"readme":"README.rdoc","changelog":null,"contributing":null,"funding":null,"license":"MIT-LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2008-11-22T01:35:28.000Z","updated_at":"2019-08-13T13:43:47.000Z","dependencies_parsed_at":"2022-07-07T23:19:32.503Z","dependency_job_id":null,"html_url":"https://github.com/tf/cached_associated_attributes","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tf%2Fcached_associated_attributes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tf%2Fcached_associated_attributes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tf%2Fcached_associated_attributes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tf%2Fcached_associated_attributes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tf","download_url":"https://codeload.github.com/tf/cached_associated_attributes/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245543887,"owners_count":20632736,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-03T17:09:09.678Z","updated_at":"2025-03-25T21:23:00.131Z","avatar_url":"https://github.com/tf.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"= CachedAssociationAttributes\n\n\u003cb\u003eSTATUS: No longer maintained / bad idea\u003c/b\u003e\n\n---\n\nEasily cache an attribute of an associated model.\n\nImagine you have a Post model that belongs_to a User model. Assume the\nUser name changes almost never. If you want to display a lot of posts\nalong with their user's names and you do not always want to :include\nthe User model, then this plugin gives you an alternative.\n\nThis plugin keeps a copy of the User.name in a column user_name on the\nposts table. When the user_id changes user_name is updated. In the\nrare case where the user.name changes all posts are updated\nautomatically. All of this happens with half a line of extra code.\n\n= Example\n\n  class Post \u003c ActiveRecord::Base\n    belongs_to :user, :cached_attributes =\u003e [:name, :email]\n  end\n\n  class User \u003c ActiveRecord::Base\n    has_many :users\n  end\n\nThis automatically adds the needed before_save and after_save\ncallbacks to the Post and User model resp to sync the user_name and\nuser_email attributes in Post:\n\n  user = User.create(:name =\u003e 'Bob', :email =\u003e 'bob@example.com')\n  post = user.posts.create\n  \n  post.user_name  # =\u003e 'Bob'\n  post.user_email # =\u003e 'bob@example.com'\n\n  user.update_attribute(:name, 'Joe')\n  post.user_name  # =\u003e 'Joe'\n\nIn the above situation you probably could just use :joins. But\ncalculated attributes are where the plugin comes in really handy:\n\n  class Post \u003c ActiveRecord::Base\n    belongs_to :user, :cached_attributes =\u003e :full_name\n  end\n\n  class User \u003c ActiveRecord::Base\n    has_many :users\n\n    def full_name\n      if first_name.blank?\n        \"#{title} #{name}\"\n      else\n        \"#{first_name} #{last_name}\"\n      end\n    end\n  end\n\n  user = User.create(:title =\u003e 'Mr', :name =\u003e 'Doe', :first_name =\u003e 'John)\n  post = user.posts.create\n  \n  post.user_full_name  # =\u003e 'John Doe'\n\n  user.update_attribute(:first_name, '')\n  post.user_full_name  # =\u003e 'Mr Doe'\n  \nNow you can show the user_full_name along with your posts without\nhaving to include the user association. If you add a *_changed? method\nPost.update_all will only be triggered if really needed:\n\n  class User \u003c ActiveRecord::Base\n    has_many :users\n\n    def full_name\n      if first_name.blank?\n        \"#{title} #{name}\"\n      else\n        \"#{first_name} #{last_name}\"\n      end\n    end\n\n    def full_name_changed?\n      name_changed? || first_name_changed? || title_changed?\n    end\n  end\n\n  # Post.update_all *will not* be called:\n  user.update_attribute(:role, 'admin')\n\n  # Post.update_all *will* be called:\n  user.update_attribute(:name, 'Jim')\n\n= Disclaimer\n\nYou might not want to overuse this feature. Keep in mind that it\nintroduces a lot of duplication in your database. This plugin is quite\nnew. Familiarize yourself with its internals if you want to use it\n(though you should probably do that with any plugin you use anyway).\n\n\nCopyright (c) 2008 Tim Fischbach, released under the MIT license\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftf%2Fcached_associated_attributes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftf%2Fcached_associated_attributes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftf%2Fcached_associated_attributes/lists"}