Question

Simply i have table

[product_categories]
name

and

[products]
category_id
name

if i use in product model

class Product < ActiveRecord::Base
attr_accessible :...
belongs_to :product_category
end

class ProductCategory < ActiveRecord::Base
  attr_accessible :...
  set_table_name "product_categories"

  has_many :products
end

i can fire

product = Product.new
product.product_category

but it is possible to rename that product_category for association for example

product.category

?

Was it helpful?

Solution

If you don't need to refer to the category with product.product_category and want to use only product.category do:

class Product < ActiveRecord::Base
  attr_accessible :...
  belongs_to :category, class_name: 'ProductCategory'
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top