Ruby on Rails Sunday, January 29, 2012

Rails 3.1.3

Hi everyone again.

I'm trying to develop a database and tables which have the association
as follows.

Video 1 ----- 0..n Script

So, in 'script.rb'

class Script < ActiveRecord::Base
belongs_to :video
...
end

and in 'video.rb'

class Video < ActiveRecord::Base
has_many :scripts
end

Furthermore, in order to refer to the parent (in this case, 'Video'), I
need to set video_id:integer for Script. Am I correct ?

in the migration file,

#--------
class CreateScripts < ActiveRecord::Migration
def change

create_table :scripts do |t|
t.references :video
t.string :text
...
end
add_index :scripts, :video_id
end
end
#---------

My question is: how can I actually associate a new 'script' entry with
its parent, 'video', in making a new 'Script' entry ?

For example, in 'index.html.erb' for NewScript page, I tried to put

<td><%= script.video_id %></td>

and in script_controller.rb

@script.video = Video.where(:video_id)

But certainly does not seem to work at all.
Could anyone show me how to set up 'html.erb' and 'controller.rb' to
associate a new 'Script' entry with its parent specifically ?

Thanks in advance.

soichi

--
Posted via http://www.ruby-forum.com/.

--
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@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment