Question

In Rails's ActiveRecord we can do things such as before_filter or before_create.

I have a case that when a certain class of data is retrieved, thus from certain model, the accessor need to be recorded. How to implement this in Rails? What filter to use? Even if there is no filter, can I know certain method that can be used for this kind of task?

Updated:

For example. There is a model called "Document" that this model need a high security. Nevertheless, when any user access record from this model, he need to be recorded. In such case, can I use some filter so that before a record is retrieved, I am able to record the user in question. Even if there is no any filter for that, may I know any other techniques that I can employ?

Sorry I am not an English speaker. :)

Was it helpful?

Solution

You actually need to have some extra logic to an existing model. What you can do is extract that extra logic to a service object which would wrap your original model with the desired behavior.

eg:

@wrapped_ticket = TicketWrapper(a_ticket)

and

class WrappedTicket

  def initialize(ticket)
    @wrapped_ticket
    ..
  end

  def get_ticket
    record_access
    return @wrapped_ticket
  end

  private

  def record_access
    ...
  end
end

you only use TicketWrapper objects and collections and TicketWrapper has all the desired functionality

I suggest you read this article for more info.

OTHER TIPS

Ohellair (read: Hallo in POSH accent).

Well. Actually ActiveRecord have the proper callback, haven't yet tried but have a look at http://guides.rubyonrails.org/active_record_callbacks.html after_initialize and after_find.

I am sorry. I think I should really try to read documentation before asking. (You know, that some documentation not really that up-to-date right? I get used to that. And I new to Rails. So sorry for 'duplicating' if it is).

Thanks.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top