Skip to content

Attaching other data to moderations

Jan Berdajs edited this page May 13, 2013 · 6 revisions

If you need any special data attached to the Moderation model, you can use the moderation_creating hook.

Attaching user ID to moderations

For example you have a Comment model, and it is moderated. But your visitors are logged in when they post comments, so you want to add the user ID to the moderation.

  • first create a migration to add user_id into Moderation

    rails g migration AddUserIdToModerations user_id:integer
    
  • rake db:migrate

  • in models/comment.rb, add something like this:

    attr_accessor :moderation_user_id
    moderation_creating do |m|
      m.user_id = moderation_user_id
    end
    
  • in your controller:

    c = Comment.new
    c.moderation_user_id = current_user.id
    c.save
    

This is just an example of how you could do this. You need the attr_accessor because we are passing the user ID from the controller. In the hook you have access to the Moderation model just before it is saved, so you can modify it like any other model.