How to write trigger in ruby on rails on creation - ruby-on-rails

I want that when new entry insert in Logs model column of another model updated after that.
For this I want to write trigger on insertion but I cant find any helping material how to write trigger in rails.

Did you try Callbacks? It seems like this is what you are after.
Maybe something like this:
class Post < ActiveRecord::Base
after_save :add_log_entry
private
def add_log_entry
LogModel.create(message: 'post thingy')
end
end

Related

Historic table in Ruby on Rails

I'm new in Ruby On Rails, so, this is probably going to be a stupid question.
I need to create a table to save the history of the transitions of an attribute. The problem is that transition it's in a diferent class from the one where I'm creating the table.
It goes something like this:
class Family::Parent
class Historic
def add_historic
#code
end
end
class Family::Parent < ActiveRecord::Base
def make_transition
#code
end
end
I know I can use something like ActiveModel::Dirty, but I have no idea how to do it, do you have any idea aout this ?
Thank you!
easiest way is to use audited gem
SEE
https://github.com/collectiveidea/audited

Rails save copy of original when model is edited

I have a simple scaffold:
rails g scaffold Order name:string notes:text
I would like for the user to be able to edit their order but keep copies of the past versions for reference. I considered adding a separate "Revision" model that would hold the text instead, but I would also like the form to stay populated with the most recent information for easy editing.
sorry for not posting more code, I'm not sure what would be helpful
I think that the better idea is create two models: Order and OrderHistory
Order will have a relation 1 to much to OrderHistory.
In update action of your OrderController you have to create a OrderHistory with the previous data and with foreign key pointing to Order. Also you can use default rails fields like create_date for getting the last revised version and mantain a good history of them.
Is a bad practise use Model Order for do that as I said.
class OrderController < ApplicationController::Base
before_action :set_order #implement this function
def update
orderHistory = OrderHistory.create(#order.params) #this code can fail I don't remmember how exactly do that now
respond_to do |format|
if #order.update(order_params)
orderHistory.save
else
#response in fail case
end
end
end
end

How to properly create a associated record just after the associator record creation?

I am using Ruby on Rails 4.1 and I would like to know if it is good and what are the drawbacks of creating a associated record just after the associator record creation. That is, for example, I have the Article class and the Comment class that belongs_to Article and I would like to create a "default" comment just after an article is created.
Probably I can use a callback method to accomplish that, but are there other approaches to accomplish what I looking for? What should I look at to be "balanced" (eg: class/module dependencies)?
You are looking for something along the lines of...
Class Article
has_many :comments
after_create :create_first_comment!
def create_first_comment!
comments.create
end
end

How can I achieve a functionality similar to "auto-insertion of timestamps"

I have a couple of fields created_by and updated_by in most of my tables. This would contain the user id of the user who created or updated the Object. is it possible to have a similar function like how rails handles created_at? I basically want it to function the same way as the timestamps insertion. I should be able to define in the columns in the migration script and configure rails to fetch the user object from a helper method everytime when it changes the particular object. Is there a direct way to do it or is there a plugin which does this?
Also you can do this without gems but with Rails Observers
You can create observer like this:
class UserTouchObserver < ActiveRecord::Observer
observe :product, :post, :comment
def after_create(model)
update_attribute(:created_by, current_user.id) if model.respond_to?(:created_by)
end
def after_update(model)
update_attribute(:updated_by, current_user.id) if model.respond_to?(:updated_by)
end
end
I was able to find a few plugins on github that do just this:
https://github.com/jnunemaker/user_stamp
https://github.com/bokmann/userstamp_basic

How to invoke a method every time database is modified

I am writing a Ruby on Rails app and I want a method to be called every time the database is modified. Is it possible to do this without inserting a method call in every location where the database is modified?
I like KandadaBooggu's answer but if you did not want to monkey with AR you might be able to do this with an Observer.
class AllObserver < ActiveRecord::Observer
observe :model_a, :model_b
def after_save(record)
logger.info("CREATED #{record.class}")
end
def after_update(record)
logger.info("UPDATED #{record.class}")
end
end
Just add the models that you want to observer. In this example it will log updates to ModelA and ModelB
Depends on the database. Many databases have very powerful stored procedure languages that can, among other things, invoke web services.
You could have a trigger on the important database tables call a ruby web service that calls your method.
Or you can have triggers that update an event table, and then have a process that watches for changes on that table and then fires the method.
There's likely some meta-programming magic that you might be able to use to tweak your ruby code to invoke the change as well.
All sorts of options.
If you want to log all models:
Monkey patch the ActiveRecord::Base class.
class ActiveRecord::Base
after_save :log_something
after_destroy :log_something
private
def log_something
end
end
For a specific model:
class User < ActiveRecord::Base
after_save :log_something
after_destroy :log_something
private
def log_something
end
end
Have you considered using: after_update or before_update in ActiveRecord:
http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html

Resources