Subject: Re: [Rails] Re: Parent id for find_or_create method

On 22.02.2012, at 15:07, Soichi Ishida wrote:

> Thanks for your answer.
>
>>
>> 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.


class ScriptsController < ApplicationController

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

end


in Action View side:

<%= render :partial => "create_or_update_script",
:locals => { :script => @script } %>

you can write lazy style (without :locals):

<%= render "create_or_update_script", :script => @script %>



So, in ActionView runtime @script has been initialized, prepared and ready to
be processed.

--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@xxxxxxxxxxxxxxxxx
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@xxxxxxxxxxxxxxxxx
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

(C)2011 mailinglist-archive.com