which works fine. Now I would like to develop further. The Script
objects may have already existed and if so, I want to update after
editing them.
So I tried,
But in general, operating any ActiveRecord model from a view is a bad idea because of rendering speed. All models should be ready to be accessed before ActionView - in controller - that would be much much faster.
But in general, operating any ActiveRecord model from a view is a bad
idea because of rendering speed. All models should be ready to be
accessed before ActionView - in controller - that would be much much
faster.
Can you (or anyone) give me more specific examples?
The segments of actual codes or links to those that show such examples
will be appreciated.
def new_or_edit_by_video
@video = Video.find(params[:video_id]) # This is helpful if the :video_id is invalid video, raises ActiveRecord::RecordNotFound
@script = Script.find_or_create_by_video_id(@video.id)
# or more optimized approach
# @script = @video.scripts.first||@video.scripts.build # or .build_script if :has_one instead of :has_many.
# ^^^ or any index due to your app-logic.
end