Hi, New to Rails here and I''m trying to determine the correct method to add a ''Post'' to multiple ''categories''. I have a select list that allows you to select multiple categories to store your post in, it translates into {"category"=>{"id"=>["1", "3"]} I have the relations set up in my model: class Post < ActiveRecord::Base has_and_belongs_to_many :categories, :join_table => "map_category_post" belongs_to :user end What I''m confused about is how to save this to the database? def create @post = Post.new(@params[:post]) #I''ve tried a few things here, but was unsuccessful. if @post.save flash[''notice''] = ''Post was successfully created.'' redirect_to :action => ''list'' else render_action ''new'' end end Many thanks, -Justin
Justin wrote:> Hi, New to Rails here and I''m trying to determine the correct method > to add a ''Post'' to multiple ''categories''. > > I have a select list that allows you to select multiple categories to > store your post in, it translates into {"category"=>{"id"=>["1", "3"]} > > I have the relations set up in my model: > > class Post < ActiveRecord::Base > has_and_belongs_to_many :categories, :join_table => "map_category_post" > belongs_to :user > > end > > What I''m confused about is how to save this to the database?This ought to do it:> def create > @post = Post.new(@params[:post]) > #I''ve tried a few things here, but was unsuccessful.@post.categories.clear @params["category"]["id"].each do |id| @post.categories << Category.find(id) end> if @post.save > flash[''notice''] = ''Post was successfully created.'' > redirect_to :action => ''list'' > else > render_action ''new'' > end > end
Look at the examples in the ACL controller example here: http://wiki.rubyonrails.com/rails/show/AccessControlListExample It does a good job of explaininng. My guess is something like this though: def create @post = Post.new(@params[''post'']) categories = @params[''post''][''categories''][''id''] categories.each do |category| @category = Category.find(id) @post.categories<<@category end if @post.save flash[''notice''] = ''Post was successfully created'' redirect_to :action => ''list'' else render_action ''new'' end end You didn''t say if the categories needed to be created if the don''t exist. But this should be close. jim On Fri, 2005-04-15 at 20:40 -0500, Justin wrote:> Hi, > New to Rails here and I''m trying to determine the correct method to > add a ''Post'' to multiple ''categories''. > > I have a select list that allows you to select multiple categories to > store your post in, it translates into {"category"=>{"id"=>["1", "3"]} > > I have the relations set up in my model: > > class Post < ActiveRecord::Base > has_and_belongs_to_many :categories, :join_table => "map_category_post" > belongs_to :user > > end > > What I''m confused about is how to save this to the database? > > def create > @post = Post.new(@params[:post]) > #I''ve tried a few things here, but was unsuccessful. > if @post.save > flash[''notice''] = ''Post was successfully created.'' > redirect_to :action => ''list'' > else > render_action ''new'' > end > end > > > Many thanks, > -Justin > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-- Jim Ray <jim-rhf1kIDhBaeB8E1WFlbJj6xOck334EZe@public.gmane.org>
Or even: def create @post = Post.new(@params[:post]) if @dj.save @dj.posts << Category.find(@params[''category'']) flash[''notice''] = ''New post saved.'' redirect_to :action => ''list'' else # Tell user the save failed. end end -- Regards, Charles.
Total Rails newbie here; I have a table named `blog_categories` and am trying to create scaffold code via:> ruby script\generate scaffold BlogCategoryand am getting this error: error Before updating scaffolding from new DB schema, try creating a tab le for your model (BlogCategory) I''ve also tried:> ruby script\generate scaffold BlogCategories > ruby script\generate scaffold Blog_CategoryAnd always get similar errors. Any tips? Is there a comprehensive guide to naming conventions for ActiveRecord? Most of the tutorials I''ve seen describe single-word table names which hasn''t helped me out. Thanks
Mike Evans <mike-u4reGlrcmwqg/H9aKuUo6lBd07bTekyB@public.gmane.org> writes:> error Before updating scaffolding from new DB schema, try creating a tab > le for your model (BlogCategory)Are you certain the blog_categories table exists in the database defined in config/database.yml and that it has a scheme? Can you show the scheme here? -- doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org
Quoting Doug Alcorn <doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org>:> Mike Evans <mike-u4reGlrcmwqg/H9aKuUo6lBd07bTekyB@public.gmane.org> writes: > >> error Before updating scaffolding from new DB schema, try creating a tab >> le for your model (BlogCategory) > > Are you certain the blog_categories table exists in the database > defined in config/database.yml and that it has a scheme? Can you show > the scheme here? > --Yes, I triple checked... here''s the table: DROP TABLE IF EXISTS `blog_categories`; CREATE TABLE `blog_categories` ( `id` smallint(5) unsigned NOT NULL auto_increment, `category` varchar(20) NOT NULL default '''', `created_on` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, `updated_on` timestamp NOT NULL default ''0000-00-00 00:00:00'', PRIMARY KEY (`id`), UNIQUE KEY `category_key` (`category`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COMMENT=''List of categories'' AUTO_INCREMENT=6 ; Does it matter which form I use?:> ruby script\generate scaffold BlogCategories > ruby script\generate scaffold BlogCategory
> Does it matter which form I use?: > > ruby script\generate scaffold BlogCategories > > ruby script\generate scaffold BlogCategoryYou should use singular for model names, and plural (with underscores) for table names. Thus... BlogCategory -> blog_categories Tomas
Ok, that makes sense. But any idea why I keep getting this?: error Before updating scaffolding from new DB schema, try creating a tab le for your model (BlogCategory) When I do have a table called `blog_categories`? Quoting Tomas Jogin <tomasj-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:>> Does it matter which form I use?: >> > ruby script\generate scaffold BlogCategories >> > ruby script\generate scaffold BlogCategory > > You should use singular for model names, and plural (with underscores) > for table names. > > Thus... BlogCategory -> blog_categories > > Tomas > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
> When I do have a table called `blog_categories`?When you have a model called BlogCategory. Regards, Tomas> > You should use singular for model names, and plural (with underscores) > > for table names. > > > > Thus... BlogCategory -> blog_categories > >
Tomas Jogin <tomasj-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:>> When I do have a table called `blog_categories`? > > When you have a model called BlogCategory.We keep dancing around his problem. He claims he has (and has posted the scheme for) a blog_categories table. He also claims he has a model called BlogCategory. He''s still getting an error when he generates the scaffolding complaining there''s no scheme for the table. You can scroll back up through this tread to see the details of the error. Assuming what he has said is true, I don''t know why the scaffold generator would give that error. -- doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org
So sorry, I misread him. He wrote "When I do...?", but I read it as "When do I...?" Tomas On 4/20/05, Doug Alcorn <doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org> wrote:> Tomas Jogin <tomasj-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes: > > >> When I do have a table called `blog_categories`? > > > > When you have a model called BlogCategory. > > We keep dancing around his problem. He claims he has (and has posted > the scheme for) a blog_categories table. He also claims he has a > model called BlogCategory. He''s still getting an error when he > generates the scaffolding complaining there''s no scheme for the > table. You can scroll back up through this tread to see the details > of the error. Assuming what he has said is true, I don''t know why the > scaffold generator would give that error. > -- > doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org >
On 19/04/2005, at 10:32 PM, Mike Evans wrote:> I have a table named `blog_categories` and am trying to create > scaffold code > via: >> ruby script\generate scaffold BlogCategory > and am getting this error: > > error Before updating scaffolding from new DB schema, try creating a > tab > le for your model (BlogCategory)Have you checked your db settings etc? Is the table in correct db, _development and not _test? Have you been sucessful generating scaffold for other tables? Have you tried deleting and recreating the said table? If none of the above helps, add the -t option to the scaffold command to get a backtrace, and post that here too. - tim lucas
Sorry about all the confusion ;-) Well, everyone is right, I''ve narrowed the problem down to the db connection. in my database.yml: development: adapter: mysql database: mike_blog host: dev.arl.arizona.edu username: mike password: ******* It doesn''t matter what database name I give (including the right one, that does contain the right table)... I get the same error, but if I change the connection (host, username, password) parameters, I get a different db connection error. So, there''s got to be something wrong here. I''m going to try installing rails on another machine and try running some tests, but if someone has an idea, please chime in. Quoting Tim Lucas <t.lucas-l/qNJNvq70OzaBltdDZI6w@public.gmane.org>:> On 19/04/2005, at 10:32 PM, Mike Evans wrote: >> I have a table named `blog_categories` and am trying to create scaffold code >> via: >>> ruby script\generate scaffold BlogCategory >> and am getting this error: >> >> error Before updating scaffolding from new DB schema, try creating a tab >> le for your model (BlogCategory) > > Have you checked your db settings etc? > Is the table in correct db, _development and not _test? > Have you been sucessful generating scaffold for other tables? > Have you tried deleting and recreating the said table? > > If none of the above helps, add the -t option to the scaffold command > to get a backtrace, and post that here too. > > - tim lucas > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Mike Evans <mike-u4reGlrcmwqg/H9aKuUo6lBd07bTekyB@public.gmane.org> writes:> host: dev.arl.arizona.eduTo make things simple, you might try running rails on the host where mysql is. In other words, run rails on localhost. -- doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org