Thursday, 15 April 2010

Issue with rails belongs_to -



Issue with rails belongs_to -

hey guys trying figure out why not work. relationship does

belongs_to :product_category, :foreign_key => :category_id

and 1 not

belongs_to :category, :class_name => :product_category, :foreign_key => :category_id

the error message "nameerror: uninitialized constant product::product_category"

why that? thanks!

the latter illustration not work because there no class called product_category. providing wrong class name. class names in ruby should written in camelcase. when rails looks product_category class it's not going find it.

your first illustration works because rails infers name of class name of relationship.

belongs_to :product_category, :foreign_key => :category_id

it converts product_category productcategory. can same thing yourself. open terminal , type following.

'product_category'.camelize.constantize

you should pass in string instead:

belongs_to :category, :class_name => 'productcategory', :foreign_key => :category_id

but in case redundant since rails can infer class name. class_name argument should used when class name can not inferred relationship name.

ruby-on-rails ruby-on-rails-4 belongs-to

No comments:

Post a Comment