Parent id for find_or_create method

Rails 3.1.3

I have tables, Video and Script having association,

Video 1 — n Script

So, every Script needs the parent id, in this case, video_id.

If I simply create a new Script instance, the view renders as follows.

<%= render :partial => "new_script", :locals => { :script =>

Script.new(:video_id => @video.id)} %>

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,

<%= render :partial => “create_or_update_script”,
:locals => { :script => Script.find_or_create_by_video_id(:video_id =>
@video.id)} %>

renders a partial,

<%= form_for script,
:url=>{:controller=>‘scripts’, :action=>‘create_or_update’},
:remote => true do |f| %>
<%= f.hidden_field :video_id %>
<%= f.text_field :startp, :readonly => true %>
<%= f.text_field :text %>
<%= f.submit “create_or_update” %>
<% end %>

But this will set “Script id” to be “Video id”, which are supposed to be
distinct from each other.

I assume the problem is the way I use “create_or_update”.
Could anyone tell me where the problem is and hopefully the solution?

Thanks in advance.

soichi

On 22.02.2012, at 6:33, Soichi I. wrote:

Rails 3.1.3

I have tables, Video and Script having association,

<%= render :partial => “create_or_update_script”,
:locals => { :script => Script.find_or_create_by_video_id(:video_id =>
@video.id)} %>

.find_or_create_by_video_id(@video.id)

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.

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.

soichi

On 22.02.2012, at 15:07, Soichi I. wrote:

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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs