Comments (13)
ok, should be simple enough to create a loop, since you can reference each version via the version number. I think I've got a solid picture of what I need to do now. Thank you for the help
from logidze.
Any update to this, was trying to switch from papertrail and would like to get this started
from logidze.
@Adeering Hi! You can create a file config/initializers/logidze.rb
and put Logidze.associations_versioning = true
there
from logidze.
Ok so I created the file and turned it on, but it doesnt seem to be working. Both models have the has_logidze and independently they log the changes. I have a recipe table and a recipe_ingredient table. The recipe has_many recipe_ingredients. When I update a recipe_ingredient it saves the update in the log, but the log count in the recipe log_data doesnt change
from logidze.
@Adeering can you please provide actual code, that you running? With the notes, what results are you expecting, and what you actually get
from logidze.
Sure here is the code:
My Recipe Model:
class Recipe < ApplicationRecord
has_logidze
#sets a lot of ingredients to 1 recipe
has_many :recipe_ingredients, inverse_of: :recipe, dependent: :destroy
Recipe Ingredients Model:
class RecipeIngredient < ApplicationRecord
has_logidze
belongs_to :recipe
And then my recipes controller:
class RecipesController < ApplicationController
before_action :set_recipe, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, except: [:index, :show]
around_action :set_logidze_responsible, only: [:create, :update]
def set_logidze_responsible(&block)
Logidze.with_responsible(current_user&.id, &block)
end
def update
respond_to do |format|
if @recipe.update(recipe_params)
format.html { redirect_to @recipe, notice: 'Recipe was successfully updated.' }
format.json { render :show, status: :ok, location: @recipe }
else
format.html { render :edit }
format.json { render json: @recipe.errors, status: :unprocessable_entity }
end
end
end
Lastly the logidze.rb in the initializers:
Logidze.associations_versioning = true
So I might be misunderstanding what is going to happen with the associated records, but expecting it to create a new version in the recipe log_data column whenever an ingredient changes. Right now it saves a new version of the ingredient in the ingredients log_data column but nothing changes in the recipe log_data column.
What im trying to do is create a view so the user can cycle thru all the changes and see what has changed. To do that I need to have a base reference as to which changes to call out for a set of changes, so was thinking that would be put in the recipes log_data column
from logidze.
@Adeering I am afraid, it's not, how it works.
When you change ingredients, no new log_data
entries in the recipe are created. Instead, when you “rewind” the recipe to the particular time, and then access its ingredients, in that moment ingredients will be “rewinded” to the same time automagically
from logidze.
Ok, so if the only things that are changing are the ingredients, is there a way I could cycle thru and show each time there is a change? More than just 1 ingredient can change at a time also
from logidze.
I guess, there is no 100% reliable way to do that. You can do something like this, of course:
recipe.recipe_ingredients.each do |ingredient|
ingredient.log_data.versions.each do |v|
# here do something with ingredient.at(v.time)
end
end
But this approach doesn't take into account deleted ingredients, the won't be shown
@palkan maybe, you've got some ideas here? Am I missing something?
from logidze.
I have the following idea:
# Custom enumerator
recipe.each_version(includes: :recipe_ingredients) do |(timestamp, recipe_at_the_time)|
# here recipe_at_the_time is just recipe.at(timestamp)
end
# To solve the problem of multiple changes within a short amount of time,
# we can provide a threshold option: combine changes which are less 1 minute one from each other.
recipe.each_version(include: :recipe_ingredients, threshold: 1.minute) do
...
end
Logidze doesn't have a special support for deleted association records. But you can combine it with paranoia
gem and do something like that:
class Recipe < ApplicationRecord
acts_as_paranoid
# add another association that takes into account deleted records
has_many :recipe_ingridients_with_deleted, -> { with_deleted }
end
# and then
recipe.each_version(include: :recipe_ingredients_with_deleted) do
...
end
@Adeering What do you think about the idea? Does it solve your problem?
from logidze.
Ok, so I think I figured out a way to do this. So in my case Im not trying to track a normal edit from a user. What happens is users vote on changes to recipes and if they get enough votes they get applied and change the recipe. I already track how many changes get approved for my search functions....originally I blacklisted all my counter indexes, but I can whitelist the successful change count and that will trigger a version in the recipe to be created. Then use the enumerator you suggested to display them.
So new question...how can I go back and change the whitelist?
from logidze.
how can I go back and change the whitelist?
Just released v0.5.3 which add support for upgrade migrations (see #46). Just run generator with --update
option:
rails generate logidze:model recipe --update --whitelist=...
And run db:migrate
to re-create trigger.
Then use the enumerator you suggested to display them
Just to clarify: the enumerator above is just a proposal, it's not there yet) If you would like to implement it – feel free to submit a PR)
from logidze.
Glad to help you) Closing the issue.
from logidze.
Related Issues (20)
- Tracking changes on JSONB column error
- How to list all versions of a record? HOT 4
- Association versioning with ignore_log_data and at(version: ) doesn't return expected results HOT 5
- Partition-friendly logging (triggers) HOT 3
- JSONB column and switch_to! with append: true HOT 2
- PG::UndefinedFunction: ERROR: function hstore(model_name) does not exist HOT 1
- Meta per request HOT 1
- Logidze.ignore_log_data_by_default causing db:migrate errors HOT 2
- Responsible from different sources HOT 3
- Associations versioning - accessing versions with `at(version: #)` HOT 2
- How to tell logidze about a new column HOT 8
- GlobalID support for metadata
- `responsible_id` is not updated on relation with `touch: true` HOT 6
- reload_log_data doesn't work when used with acts_as_paranoid and deleted records HOT 2
- docs: logdize cannot set session meta data when using a connection pool HOT 1
- How can I get the last 100 versions of a model? regardless of the record HOT 2
- Saving log with only metadata changes HOT 1
- Not possible to use latest logidze and anyway_config with Ruby 3.3.0 HOT 1
- Make Sequel support independent from ActiveRecord and ActiveSupport
- Updating history when column names change HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from logidze.