开发者

How do I implement this code without needing to access current_user.id from a model in RoR?

I have a note model, with the following association

note.rb

has_many :note_categories, :dependent => :destroy
has_many :categories, :through => :note_categories

The NoteCategory model was created to function as a join table between notes and categories.

I need to implement the following:

  1. A user removes a category from a note. This can be done by either removing one category from a note (deletes one entry in the note_categories table), or by deleting the note entirely (deletes all entries in the note_categories table relating to the note)
  2. Before the row/s in note_categories i开发者_运维技巧s/are deleted, I need to determine if the user who is deleting the category from a note is the same user who initially created the category (creator field in the category model)
  3. If it is the same user, the category entry itself is to be deleted

Obviously to do this, I need to access the id of the user, to check against the creator field of the Category. I am already using a before_destroy method in the NoteCategory model to do some other things, but I can't access current_user.id in there because it's a model, and current_user is a method in the Application Controller. From the questions I've read here on SO, it seems that accessing the id of the current user from a model is bad form.

I don't think I can use the controller in this circumstance because when a note is deleted, the :dependent => :destroy line means that the associated rows in note_categories are deleted as well. I need to do the creator check in this situation as well, but the note_categories rows are removed via the destroy method in the model, not the controller, which is the behavior specified by :dependent => :destroy.

So how should I go about doing it? Thanks for reading!


One way of doing this could be to add an attr_accessor to the Note model like so:

# in Note.rb
attr_accessor :destroyed_by

and set it before destroying the record:

# NotesController#destroy
@note.destroyed_by = current_user.id
@note.destroy

Then in your Note.rb before_destroy call, you can check the Category's creator id against the destroyed_by id.


You say

Before the row/s in note_categories is/are deleted, I need to determine if the user who is deleting the category from a note is the same user who initially created the category (creator field in the category model) If it is the same user, the category entry itself is to be deleted

Are you sure you wan't to do such a thing? What if a user has used the same category for many notes, and he wants to delete it form one of them? You sure would want to delete an entry from note_categories, but should you also delete the category itself?

A common implementation for such a scenario is to check while deleting a note_category (through perhaps a before_destroy) whether this one is the last note_categories for the category, and delete it if it is. This also means that if a note is deleted, only the related note_categories should be deleted, and not the categories themselves.


I think that you want to maintain a log of who created the post and who is deleting it. The before_destroy method does the part of deleting associations and I think it is working fine for you. as of maintaining record of whether the user who created the note is deleting the note or not comes under logging part. I hope this helps you around this problem

http://rohitsharma9889.wordpress.com/2010/08/19/logging-in-ruby-on-rails/

EDIT: You can also try reading this article. I would recommend you to prefer this one on the above one

Environment variables in Model

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜