Pardon the newbie question, but.... I have a model where a cell has_many images. My model files look like this: class Cell < ActiveRecord::Base has_many :images end class Image < ActiveRecord::Base belongs_to :cell end Both the "cells" and the "images" table have their own "id" field. The foreign key in "images" is called "cell_id". I''d like to be able to add and image to a cell. The scaffold code lets me add a record to images, but it doesn''t have any foreign key assigned (the images table defaults it to "0", which is not a cell id!). [ here''s where the newbie really starts coming out... ] How does one create the new Images record with a cell_id correctly set? Supposing that I place a link for this purpose on a cell-type page. That link would be something like "/images/new"...... how do I communicate the cell''s id (named "id" in cells table) to the images controller (where it will need to assign cell_id value). I was hoping the scaffold would just "magically" take care of this, but that doesn''t seem to be the case. Thanks! Dan Sperka
the scaffold is really just a starting point. Once you start describing relationships, you''re on your own. here''s what I do: cell = <the cell that I''m adding the image to> image = Image.new cell.images << image image.save On Apr 12, 2005 3:46 PM, Daniel Sperka <djsperka-ZnEz5tD0I2KVc3sceRu5cw@public.gmane.org> wrote:> Pardon the newbie question, but.... > > I have a model where a cell has_many images. My model files look like this: > > class Cell < ActiveRecord::Base > has_many :images > end > > class Image < ActiveRecord::Base > belongs_to :cell > end > > Both the "cells" and the "images" table have their own "id" field. The > foreign key in "images" is called "cell_id". > > I''d like to be able to add and image to a cell. The scaffold code lets > me add a record to images, but it doesn''t have any foreign key assigned > (the images table defaults it to "0", which is not a cell id!). > > [ here''s where the newbie really starts coming out... ] > > How does one create the new Images record with a cell_id correctly set? > Supposing that I place a link for this purpose on a cell-type page. That > link would be something like "/images/new"...... how do I communicate > the cell''s id (named "id" in cells table) to the images controller > (where it will need to assign cell_id value). > > I was hoping the scaffold would just "magically" take care of this, but > that doesn''t seem to be the case. > > Thanks! > > Dan Sperka > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
There has been talk in the past about adding associations to the scaffold. It''s fertile ground for a project and doesn''t require that much rails knowledge coming in. I don''t think it''s really neccesary, though. I generally generate scaffold and start hacking it up right away. If I need associations early on for testing, I stick them in fixtures or edit the db by hand (ColoaMysql or PhpMySql are good for this). The way that links between objects are established is much less regular/predictable by scaffolding than the way their data is edited, so the scaffolding won''t make much sense a lot of the time. Think about it: sometimes you want the object that has_one other_object to be adding them and sometimes the reverse is more natural. habtm is a whole other can of worms. What about general graph structures where node habtm node? There''s a lot of issues here, and covering them all in the scaffolding might make it unneccesarily complex for little or no gain. That said, a simple and elegant implementation of associations in scaffolding would be welcome. Brian On 4/12/05, George Madrid <gmadrid-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> the scaffold is really just a starting point. Once you start > describing relationships, you''re on your own. > > here''s what I do: > > cell = <the cell that I''m adding the image to> > image = Image.new > cell.images << image > image.save > > > On Apr 12, 2005 3:46 PM, Daniel Sperka <djsperka-ZnEz5tD0I2KVc3sceRu5cw@public.gmane.org> wrote: > > Pardon the newbie question, but.... > > > > I have a model where a cell has_many images. My model files look like this: > > > > class Cell < ActiveRecord::Base > > has_many :images > > end > > > > class Image < ActiveRecord::Base > > belongs_to :cell > > end > > > > Both the "cells" and the "images" table have their own "id" field. The > > foreign key in "images" is called "cell_id". > > > > I''d like to be able to add and image to a cell. The scaffold code lets > > me add a record to images, but it doesn''t have any foreign key assigned > > (the images table defaults it to "0", which is not a cell id!). > > > > [ here''s where the newbie really starts coming out... ] > > > > How does one create the new Images record with a cell_id correctly set? > > Supposing that I place a link for this purpose on a cell-type page. That > > link would be something like "/images/new"...... how do I communicate > > the cell''s id (named "id" in cells table) to the images controller > > (where it will need to assign cell_id value). > > > > I was hoping the scaffold would just "magically" take care of this, but > > that doesn''t seem to be the case. > > > > Thanks! > > > > Dan Sperka > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- The years ahead pick up their dark bags. They move closer. There''s a slight rise in the silence then nothing. -- (If you''re receiving this in response to mail sent to bluczkie-OM76b2Iv3yLQjUSlxSEPGw@public.gmane.org, don''t be concerned This is my new address, but mail will be forwarded here indefinitely)
Interesting subject line. Should it perhaps become, "Newbie has_many questions"? :) (still a newbie at most things myself) Duane Johnson (canadaduane)>
I can see a "general purpose" solution for associations breaking very quickly under very modest database sizes. It doesn''t take even a hundred entries to make a select/option combo a nightmare to use. On the other hand, like folks have said: it''s scaffold. It should be expected to work great under every circumstance. Maybe instead of placing it in the scaffold, it could be placed in generated controller code? That would provide newbies with an easy place to start, without writing in something possibly untenable. class Book < ActiveRecord has_many :pages end class Pages < ActiveRecord belongs_to :book , :foreign_key = "book_id" end class PagesController < ApplicationController ... def new @page = Page.new @books = Book.find_all end ... end - Sam On Apr 15, 2005, at 1:50 AM, Brian L. wrote: There has been talk in the past about adding associations to the scaffold. It''s fertile ground for a project and doesn''t require that much rails knowledge coming in. I don''t think it''s really neccesary, though. I generally generate scaffold and start hacking it up right away. If I need associations early on for testing, I stick them in fixtures or edit the db by hand (ColoaMysql or PhpMySql are good for this). The way that links between objects are established is much less regular/predictable by scaffolding than the way their data is edited, so the scaffolding won''t make much sense a lot of the time. Think about it: sometimes you want the object that has_one other_object to be adding them and sometimes the reverse is more natural. habtm is a whole other can of worms. What about general graph structures where node habtm node? There''s a lot of issues here, and covering them all in the scaffolding might make it unneccesarily complex for little or no gain. That said, a simple and elegant implementation of associations in scaffolding would be welcome. Brian On 4/12/05, George Madrid <gmadrid-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> the scaffold is really just a starting point. Once you start > describing relationships, you''re on your own. > > here''s what I do: > > cell = <the cell that I''m adding the image to> > image = Image.new > cell.images << image > image.save > > > On Apr 12, 2005 3:46 PM, Daniel Sperka <djsperka-ZnEz5tD0I2KVc3sceRu5cw@public.gmane.org> wrote: >> Pardon the newbie question, but.... >> >> I have a model where a cell has_many images. My model files look like >> this: >> >> class Cell < ActiveRecord::Base >> has_many :images >> end >> >> class Image < ActiveRecord::Base >> belongs_to :cell >> end >> >> Both the "cells" and the "images" table have their own "id" field. The >> foreign key in "images" is called "cell_id". >> >> I''d like to be able to add and image to a cell. The scaffold code lets >> me add a record to images, but it doesn''t have any foreign key >> assigned >> (the images table defaults it to "0", which is not a cell id!). >> >> [ here''s where the newbie really starts coming out... ] >> >> How does one create the new Images record with a cell_id correctly >> set? >> Supposing that I place a link for this purpose on a cell-type page. >> That >> link would be something like "/images/new"...... how do I communicate >> the cell''s id (named "id" in cells table) to the images controller >> (where it will need to assign cell_id value). >> >> I was hoping the scaffold would just "magically" take care of this, >> but >> that doesn''t seem to be the case. >> >> Thanks! >> >> Dan Sperka >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- The years ahead pick up their dark bags. They move closer. There''s a slight rise in the silence then nothing. -- (If you''re receiving this in response to mail sent to bluczkie-OM76b2Iv3yLQjUSlxSEPGw@public.gmane.org, don''t be concerned This is my new address, but mail will be forwarded here indefinitely) _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Doh. Obviously, that should have read, "It should NOT be expected to work great under every circumstance." And the only reason I noticed it this time was because I''ve started my caffeine dose for the morning. :) On Apr 18, 2005, at 9:29 AM, Sam Leibowitz wrote: I can see a "general purpose" solution for associations breaking very quickly under very modest database sizes. It doesn''t take even a hundred entries to make a select/option combo a nightmare to use. On the other hand, like folks have said: it''s scaffold. It should be expected to work great under every circumstance. Maybe instead of placing it in the scaffold, it could be placed in generated controller code? That would provide newbies with an easy place to start, without writing in something possibly untenable. class Book < ActiveRecord has_many :pages end class Pages < ActiveRecord belongs_to :book , :foreign_key = "book_id" end class PagesController < ApplicationController ... def new @page = Page.new @books = Book.find_all end ... end - Sam On Apr 15, 2005, at 1:50 AM, Brian L. wrote: There has been talk in the past about adding associations to the scaffold. It''s fertile ground for a project and doesn''t require that much rails knowledge coming in. I don''t think it''s really neccesary, though. I generally generate scaffold and start hacking it up right away. If I need associations early on for testing, I stick them in fixtures or edit the db by hand (ColoaMysql or PhpMySql are good for this). The way that links between objects are established is much less regular/predictable by scaffolding than the way their data is edited, so the scaffolding won''t make much sense a lot of the time. Think about it: sometimes you want the object that has_one other_object to be adding them and sometimes the reverse is more natural. habtm is a whole other can of worms. What about general graph structures where node habtm node? There''s a lot of issues here, and covering them all in the scaffolding might make it unneccesarily complex for little or no gain. That said, a simple and elegant implementation of associations in scaffolding would be welcome. Brian On 4/12/05, George Madrid <gmadrid-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> the scaffold is really just a starting point. Once you start > describing relationships, you''re on your own. > > here''s what I do: > > cell = <the cell that I''m adding the image to> > image = Image.new > cell.images << image > image.save > > > On Apr 12, 2005 3:46 PM, Daniel Sperka <djsperka-ZnEz5tD0I2KVc3sceRu5cw@public.gmane.org> wrote: >> Pardon the newbie question, but.... >> >> I have a model where a cell has_many images. My model files look like >> this: >> >> class Cell < ActiveRecord::Base >> has_many :images >> end >> >> class Image < ActiveRecord::Base >> belongs_to :cell >> end >> >> Both the "cells" and the "images" table have their own "id" field. The >> foreign key in "images" is called "cell_id". >> >> I''d like to be able to add and image to a cell. The scaffold code lets >> me add a record to images, but it doesn''t have any foreign key >> assigned >> (the images table defaults it to "0", which is not a cell id!). >> >> [ here''s where the newbie really starts coming out... ] >> >> How does one create the new Images record with a cell_id correctly >> set? >> Supposing that I place a link for this purpose on a cell-type page. >> That >> link would be something like "/images/new"...... how do I communicate >> the cell''s id (named "id" in cells table) to the images controller >> (where it will need to assign cell_id value). >> >> I was hoping the scaffold would just "magically" take care of this, >> but >> that doesn''t seem to be the case. >> >> Thanks! >> >> Dan Sperka >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- The years ahead pick up their dark bags. They move closer. There''s a slight rise in the silence then nothing. -- (If you''re receiving this in response to mail sent to bluczkie-OM76b2Iv3yLQjUSlxSEPGw@public.gmane.org, don''t be concerned This is my new address, but mail will be forwarded here indefinitely) _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails