do the form helpers work properly with legacy database schema? when i deviate from the standard rails schema... they dont produce form elements that can be passed to update_attributes _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
well dur i just had ''oh so that is how it works'' moment after digging around in the code... so ignore that question... Sean T Allen wrote:> do the form helpers work properly with legacy database schema? > > when i deviate from the standard rails schema... they dont produce > form elements that can be passed to update_attributes > > >_______________________________________________ >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
Well its obviously late and i spoke to soon... have links that belong to a category class Link < ActiveRecord::Base belongs_to :category, { :order => ''name'', :foreign_key => ''category'', :class_name => ''LinkCategory'' } end class LinkCategory < ActiveRecord::Base has_many :links, { :order => ''name'', :foreign_key => ''category'' } has_many :published_links, { :order => ''name'', :foreign_key => ''category'', :class_name => ''Link'', :conditions => ''publish 1'' } end Everyhting works until I try to create a new Link then ActiveRecord::AssociationTypeMismatch in Links#create LinkCategory expected, got String |app/controllers/links_controller.rb:33:in `new'' app/controllers/links_controller.rb:33:in `create'' | Request *Parameters*: {"submit"=>"Create", "link"=>{"name"=>"sdsa", "category"=>"2", "url"=>"asdd", "description"=>"asdsad"}} def new @link = Link.new end def create @link = Link.new( @params[ :link ] ) <-- line 33 if @link.save flash[''notice''] = ''Link was successfully created.'' redirect_to :action => ''list'' else render_action ''new'' end end The docs for belongs_to say: :class_name - specify the class name of the association. Use it only if that name can’t be inferred from the association name. So has_one <http://api.rubyonrails.com/classes/ActiveRecord/Associations/ClassMethods.html#M000434> :author will by default be linked to the Author class, but if the real class name is Person, you’ll have to specify it with this option. Which suggest to me that if instead of having category as the field name database. if a had link_category_id that it would create a LinkCategory object on new but that doesnt seem to make sense in the given situation... because them it should still work. So... if I didnt have class_name and was using link_category_id and the association was :link_category it would just be willing to accept a string? thoroughly confused and looking for help... _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 7.5.2005, at 13:09, Sean T Allen wrote:> Well its obviously late and i spoke to soon... > > have links that belong to a category > > class Link < ActiveRecord::Base > belongs_to :category, { :order => ''name'', :foreign_key => ''category'', > :class_name => ''LinkCategory'' } > end > > class LinkCategory < ActiveRecord::Base > has_many :links, { :order => ''name'', :foreign_key => ''category'' } > has_many :published_links, { :order => ''name'', :foreign_key => > ''category'', :class_name => ''Link'', :conditions => ''publish > 1'' } > end > > Everyhting works until I try to create a new Link then > > > ActiveRecord::AssociationTypeMismatch in Links#create > > LinkCategory expected, got String > > |app/controllers/links_controller.rb:33:in `new'' > app/controllers/links_controller.rb:33:in `create'' > > | > > > Request > > *Parameters*: {"submit"=>"Create", "link"=>{"name"=>"sdsa", > "category"=>"2", "url"=>"asdd", "description"=>"asdsad"}} > > def new > @link = Link.new > end > > def create > @link = Link.new( @params[ :link ] ) <-- line 33 > if @link.save > flash[''notice''] = ''Link was successfully created.'' > redirect_to :action => ''list'' > else > render_action ''new'' > end > end > > The docs for belongs_to say: > > :class_name - specify the class name of the association. Use it only > if that name can’t be inferred from the association name. So has_one > <http://api.rubyonrails.com/classes/ActiveRecord/Associations/ > ClassMethods.html#M000434> :author will by default be linked to the > Author class, but if the real class name is Person, you’ll have to > specify it with this option. > > Which suggest to me that if instead of having category as the field > name database. if a had link_category_id that it would > create a LinkCategory object on new but that doesnt seem to make sense > in the given situation... > because them it should still work. > > So... if I didnt have class_name and was using link_category_id and > the association was :link_category > > it would just be willing to accept a string? > > thoroughly confused and looking for help... > > > > <sean.vcf>_______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Argh, hit the send button unintentionally. On 7.5.2005, at 13:09, Sean T Allen wrote:> Well its obviously late and i spoke to soon... > > have links that belong to a category > > class Link < ActiveRecord::Base > belongs_to :category, { :order => ''name'', :foreign_key => ''category'', > :class_name => ''LinkCategory'' } > end > > class LinkCategory < ActiveRecord::Base > has_many :links, { :order => ''name'', :foreign_key => ''category'' } > has_many :published_links, { :order => ''name'', :foreign_key => > ''category'', :class_name => ''Link'', :conditions => ''publish > 1'' } > end > > Everyhting works until I try to create a new Link then > > > ActiveRecord::AssociationTypeMismatch in Links#create> LinkCategory expected, got StringThis is because your Link objects now have category= method which assumes you give it a LinkCategory object. That''s why it''s not a good idea to share the name between the foreign key field and the association.> > |app/controllers/links_controller.rb:33:in `new'' > app/controllers/links_controller.rb:33:in `create'' > > | > > > Request > > *Parameters*: {"submit"=>"Create", "link"=>{"name"=>"sdsa", > "category"=>"2", "url"=>"asdd", "description"=>"asdsad"}} > > def new > @link = Link.new > end > > def create > @link = Link.new( @params[ :link ] ) <-- line 33 > if @link.save > flash[''notice''] = ''Link was successfully created.'' > redirect_to :action => ''list'' > else > render_action ''new'' > end > end > > The docs for belongs_to say: > > :class_name - specify the class name of the association. Use it only > if that name can’t be inferred from the association name. So has_one > <http://api.rubyonrails.com/classes/ActiveRecord/Associations/ > ClassMethods.html#M000434> :author will by default be linked to the > Author class, but if the real class name is Person, you’ll have to > specify it with this option. > > Which suggest to me that if instead of having category as the field > name database. if a had link_category_id that it would > create a LinkCategory object on new but that doesnt seem to make sense > in the given situation... > because them it should still work. > > So... if I didnt have class_name and was using link_category_id and > the association was :link_category > > it would just be willing to accept a string?Yes, if your form field was named link_category_id. I think it would also work if you just named your association to :link_category (belongs_to :link_category). That way Link.category would always refer to the db field and not the associated object. //jarkko> > thoroughly confused and looking for help... > > > > <sean.vcf>_______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Didn''t actually get anything as a reply.. Jarkko Laine wrote:> > On 7.5.2005, at 13:09, Sean T Allen wrote: > >> Well its obviously late and i spoke to soon... >> >> have links that belong to a category >> >> class Link < ActiveRecord::Base >> belongs_to :category, { :order => ''name'', :foreign_key => >> ''category'', :class_name => ''LinkCategory'' } >> end >> >> class LinkCategory < ActiveRecord::Base >> has_many :links, { :order => ''name'', :foreign_key => ''category'' } >> has_many :published_links, { :order => ''name'', :foreign_key => >> ''category'', :class_name => ''Link'', :conditions => ''publish >> 1'' } >> end >> >> Everyhting works until I try to create a new Link then >> >> >> ActiveRecord::AssociationTypeMismatch in Links#create >> >> LinkCategory expected, got String >> >> |app/controllers/links_controller.rb:33:in `new'' >> app/controllers/links_controller.rb:33:in `create'' >> >> | >> >> >> Request >> >> *Parameters*: {"submit"=>"Create", "link"=>{"name"=>"sdsa", >> "category"=>"2", "url"=>"asdd", "description"=>"asdsad"}} >> >> def new >> @link = Link.new >> end >> >> def create >> @link = Link.new( @params[ :link ] ) <-- line 33 >> if @link.save >> flash[''notice''] = ''Link was successfully created.'' >> redirect_to :action => ''list'' >> else >> render_action ''new'' >> end >> end >> >> The docs for belongs_to say: >> >> :class_name - specify the class name of the association. Use it only >> if that name can’t be inferred from the association name. So has_one >> <http://api.rubyonrails.com/classes/ActiveRecord/Associations/ >> ClassMethods.html#M000434> :author will by default be linked to the >> Author class, but if the real class name is Person, you’ll have to >> specify it with this option. >> >> Which suggest to me that if instead of having category as the field >> name database. if a had link_category_id that it would >> create a LinkCategory object on new but that doesnt seem to make >> sense in the given situation... >> because them it should still work. >> >> So... if I didnt have class_name and was using link_category_id and >> the association was :link_category >> >> it would just be willing to accept a string? >> >> thoroughly confused and looking for help... >> >> >> >> <sean.vcf>_______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > -- > Jarkko Laine > http://jlaine.net > http://odesign.fi > >------------------------------------------------------------------------ > >_______________________________________________ >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
> > Yes, if your form field was named link_category_id. I think it would > also work if you just named your association to :link_category > (belongs_to :link_category). That way Link.category would always > refer to the db field and not the associated object. > > //jarkko >I jsut did a test... if i create a table links with link_category_id for select i have to pass <%= select ''link'', ''link_category_id'' ... which will make it work but if i have this as an association... belongs_to :category, { :order => ''name'', :foreign_key => ''link_category_id'', :class_name => ''LinkCategory'' } in link should that allow me to later do link.category.name ? in order to access the category? |undefined method `name'' for nil:NilClass is what i currently get... is is an eager loading issue? | _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Sean T Allen wrote:> >> >> Yes, if your form field was named link_category_id. I think it would >> also work if you just named your association to :link_category >> (belongs_to :link_category). That way Link.category would always >> refer to the db field and not the associated object. >> >> //jarkko >> > I jsut did a test... > > if i create a table > > links > > with > > link_category_id > > for select i have to pass > > <%= select ''link'', ''link_category_id'' ... > > which will make it work > > but > > if i have this as an association... > > belongs_to :category, { :order => ''name'', :foreign_key => > ''link_category_id'', :class_name => ''LinkCategory'' } > > in link > > should that allow me to later do > > link.category.name > > ? > > in order to access the category? > > |undefined method `name'' for nil:NilClass > > is what i currently get... > > is is an eager loading issue? > |Ok... it is an eager loading issue but i''m confused... list and show templates both contain... <%= link.category.name %> but i have def list @pages, @links = paginate :link, { :per_page => 25, :order_by => ''name'' } end def show @link = Link.find( @params[ :id ], :include => [ :category ] ) end where show requires the include but list doesnt... how does paginate get around the need to eager load? _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails