Reputation: 8631
I have my attend method below currently in my controller. My question is how do I know whether to put this in my event or user model vs. my events controller? I am also going to add another method remove_attend which will do the opposite of attend. At what point do I put these methods into a model?
def attend
@event = Event.find(params[:id])
if @event.users.include?(current_user)
flash[:error] = "You're already attending this event."
else
current_user.events << @event
flash[:success] = "Attending event!"
end
redirect_to @event
end
Upvotes: 0
Views: 157
Reputation: 837
This should be in the controller, the model should only be called upon to edit or retrieve data.
from what i'm seeing it looks like you're referencing an object (current_user) that isn't a property of the class you calling the method from, this shouldn't happen in the model.
Upvotes: 0
Reputation: 15136
I'd leave this code in the controller. If you start accessing User attributes in this code (e.g. checking the type of user or the number of events they're already attending), then it might be a good idea to move that code into the user model.
Upvotes: 0
Reputation: 48706
It belongs to the event controller. Flash messages or redirects cannot be put in the models. So, when you see either, it's safe to assume that they are controller material.
It belongs to the event controller because the resource that attend refers to is an Event. You can create, edit or attend an event, in that sense.
Upvotes: 3