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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Sun, Jan 29, 2012 at 05:45, Soichi Ishida <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Furthermore, in order to refer to the parent (in this case, ''Video''), I > need to set video_id:integer for Script. Am I correct ?Yes.> 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>I don''t understand. index.html.erb is for showing the list of something. What''s "NewScript" got to do with it? You may be on *almost* the right track though. Assuming you get to views/scripts/new.html.erb by clicking on something associated with a video, you can set the new script object''s video_id in the controller, and add a hidden field to hold it (for submission to create).> -ut+LiW8o05QSIzm9emH6NA@public.gmane.org = Video.where(:video_id)That where-clause isn''t going to work. It''s just saying "where there''s a valid video_id". Maybe you mean where :video_id => video.id (if there is a variable called video), or maybe video_id. Anyway, more direct would be simply "@script.video_id = video_id". No searching needed. Assuming of course that you already have the video_id. BTW, do you know the definition of "Where Clause"? It''s a fat guy who dresses up in a red suit and gives out presents, when the moon is full. ;-) -Dave -- Dave Aronson: Available Cleared Ruby on Rails Freelancer (NoVa/DC/Remote) -- see www.DaveAronson.com, and blogs at www.Codosaur.us, www.Dare2XL.com, www.RecruitingRants.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
Thanks for your explanation. When I am confused, it is also difficult to explain what I understand and what I don''t. But your guess is right.>"@script.video_id = video_id"Well, this worked as I wished. It was simpler than I thought! Thanks again. 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Tue, Jan 31, 2012 at 02:28, Soichi Ishida <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> It was simpler than I thought!Rails often is. For example, a while back I was trying to do a simple timesheet system. Couldn''t figure out how to force the newly entered entries into the right places. Came up with a hairy and fragile way, that duplicated a lot of what Rails was doing behind the scenes. Correct solution? Don''t. Just let Rails do it. No problem. Now I use essentially the same technique in The Decider (http://thedecider.herokuapp.com), to let Rails keep track of which rating in the spreadsheet is for which alternative in which factor.> Thanks again.You''re welcome. -Dave -- Dave Aronson: Available Cleared Ruby on Rails Freelancer (NoVa/DC/Remote) -- see www.DaveAronson.com, and blogs at www.Codosaur.us, www.Dare2XL.com, www.RecruitingRants.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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.