Jessica Lu
2013-May-09 21:41 UTC
Auto generate models on creation of other models in Ruby on Rails
I have a model called Video that has many statuses. I want to create a status of each kind and add it to @video.statuses in the def create of VideosController. In VideosController, I have: def create @video = Video.new(params[:video]) Kind.all.each do |f| @video.statuses.new(:kind =>f, :kind_id=>f.id,:comment =>"", :time_comp => nil, :completed =>false, :video_id =>@video.id) end respond_to do |format| if @video.save format.html { redirect_to @video, notice: ''Video was successfully created.'' } format.json { render json: @video, status: :created, location: @video } else format.html { render action: "new" } format.json { render json: @video.errors, status: :unprocessable_entity } end end end However, this returns an error that says that the video could not be saved because the Statuses were invalid. The only validation in my entire project is in status and it merely checks that there is a video_id. I''m very confused why I''m getting this error and would appreciate any help! https://github.com/ninajlu/videos -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/V3dUCt6Vl-0J. For more options, visit https://groups.google.com/groups/opt_out.
Colin Law
2013-May-10 09:09 UTC
Re: Auto generate models on creation of other models in Ruby on Rails
On 9 May 2013 22:41, Jessica Lu <loppilop-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I have a model called Video that has many statuses. I want to create a > status of each kind and add it to @video.statuses in the def create of > VideosController. > > In VideosController, I have: > > def create > @video = Video.new(params[:video]) > Kind.all.each do |f| > @video.statuses.new(:kind =>f, :kind_id=>f.id,:comment =>"", > :time_comp => nil, :completed =>false, :video_id =>@video.id)I think you probably want @video.statuses.create( .. ) so that the status objects will be saved. See the Rails Guide on ActiveRecord Associations for more detail. Colin> end > respond_to do |format| > if @video.save > format.html { redirect_to @video, notice: ''Video was successfully > created.'' } > format.json { render json: @video, status: :created, location: > @video } > else > format.html { render action: "new" } > format.json { render json: @video.errors, status: > :unprocessable_entity } > end > end > end > > However, this returns an error that says that the video could not be saved > because the Statuses were invalid. The only validation in my entire project > is in status and it merely checks that there is a video_id. I''m very > confused why I''m getting this error and would appreciate any help! > > https://github.com/ninajlu/videos > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-talk/-/V3dUCt6Vl-0J. > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Carlos Mathiasen
2013-May-10 10:36 UTC
Re: Auto generate models on creation of other models in Ruby on Rails
On Fri, May 10, 2013 at 5:09 AM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 9 May 2013 22:41, Jessica Lu <loppilop-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I have a model called Video that has many statuses. I want to create a > > status of each kind and add it to @video.statuses in the def create of > > VideosController. > > > > In VideosController, I have: > > > > def create > > @video = Video.new(params[:video]) > > Kind.all.each do |f| > > @video.statuses.new(:kind =>f, :kind_id=>f.id,:comment =>"", > > :time_comp => nil, :completed =>false, :video_id =>@video.id) >You don''t need ":video_id => @video.id". Replace this line with that: @video.statuses.build(:kind =>f, :kind_id=>f.id,:comment =>"", :time_comp => nil, :completed =>false) build is an alias to new, you can look here[0] Then when you call @video.save, all statuses will create too, with @video.id . In your code @video is not saved when you call to statuses.new, then @ video.id is nil, that''s why of your error [0] http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html -- Matt''s -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Colin Law
2013-May-10 10:49 UTC
Re: Auto generate models on creation of other models in Ruby on Rails
On 10 May 2013 11:36, Carlos Mathiasen <gunmath987-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Fri, May 10, 2013 at 5:09 AM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> >> On 9 May 2013 22:41, Jessica Lu <loppilop-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> > I have a model called Video that has many statuses. I want to create a >> > status of each kind and add it to @video.statuses in the def create of >> > VideosController. >> > >> > In VideosController, I have: >> > >> > def create >> > @video = Video.new(params[:video]) >> > Kind.all.each do |f| >> > @video.statuses.new(:kind =>f, :kind_id=>f.id,:comment =>"", >> > :time_comp => nil, :completed =>false, :video_id =>@video.id) > > > You don''t need ":video_id => @video.id". Replace this line with that: > @video.statuses.build(:kind =>f, :kind_id=>f.id,:comment =>"", :time_comp => > nil, :completed =>false) > build is an alias to new, you can look here[0] > > Then when you call @video.save, all statuses will create too, with > @video.id.Carlos is right, my suggestion to use create will not work as the video has not been saved yet. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.