Is line number 3 in my create action considered bad practice?
Because a user has many photo_albums and an album needs to be linked to the right user I basically grab the id from current_user and store it in the user_id column of my photo_albums database table.
I’m sure this should be done from the model e.g before_save or before_create
I’m having trouble getting it to work. I created a method with an argument and tried to pass the current_user id into that method via the controller to the method written in my model then supplied that method name after before save but I either get wrong number of arguments error or undefined method.
Can some one explain how I can do this thanks very much.
def create
@photoalbum = PhotoAlbum.new(params[:photo_album])
@photoalbum.user_id = current_user.id
if @photoalbum.save
flash[:notice] = "Successfully created gallery."
redirect_to @photoalbum
else
render :action => 'new'
end
end
Kind regards
There’s nothing wrong with this,
but I’d be inclined to make it more specific.
You say a user
has_many :photo_albumSo instead we can say
This achieves the same thing,
but using the has_many makes it clearer our intent.