É
Éric DUMINIL
Hi everybody!
Here are trunks of models from a little rails app I'm developing:
###########################
class Country < ActiveRecord::Base
has_many :companies
........
###########################
class Category < ActiveRecord::Base
has_many :companies
........
###########################
class Company < ActiveRecord::Base
has_many :employees , :dependent => true
belongs_to :country, :counter_cache => true
belongs_to :category, :counter_cache => true
........
###########################
class Employee < ActiveRecord::Base
belongs_to :company, :counter_cache => true
........
###########################
I just would like to know: The right way to create a new company by updating
Country.find(id).companies.size is by doing
Country.find(id).companies.create . Right?
1) But what if I'd like to keep in cache companies.size for the category in
which the new company is put? I can't create a company simultaneously using
two parents!
2) When I edit the properties of a company, let's say moving from Japan to
Italy, how could I update Japan.companies_count and Italy.companies_count?
I tried to modify the update action of CompanieController:
###########################
def update
@company = Company.find(params[:id])
@[email protected]
if @company.update_attributes(params[:company])
@[email protected]
@[email protected]refresh).size
@[email protected]refresh).size
flash[:notice] = 'Company was successfully updated.'
redirect_to :action => 'show', :id => @company
else
render :action => 'edit'
end
end
###########################
but that did'nt work.
3) Is there a Rails' way to cache the number of Employees directly in
Company.employees_count and Category.employees_count (i.e. through
a "has_many_many" relation)? That would prevent iterating through every
company of a given country/category to get its .employees_count!
Thanks a lot for your attention,
Have a good night,
Eric
Here are trunks of models from a little rails app I'm developing:
###########################
class Country < ActiveRecord::Base
has_many :companies
........
###########################
class Category < ActiveRecord::Base
has_many :companies
........
###########################
class Company < ActiveRecord::Base
has_many :employees , :dependent => true
belongs_to :country, :counter_cache => true
belongs_to :category, :counter_cache => true
........
###########################
class Employee < ActiveRecord::Base
belongs_to :company, :counter_cache => true
........
###########################
I just would like to know: The right way to create a new company by updating
Country.find(id).companies.size is by doing
Country.find(id).companies.create . Right?
1) But what if I'd like to keep in cache companies.size for the category in
which the new company is put? I can't create a company simultaneously using
two parents!
2) When I edit the properties of a company, let's say moving from Japan to
Italy, how could I update Japan.companies_count and Italy.companies_count?
I tried to modify the update action of CompanieController:
###########################
def update
@company = Company.find(params[:id])
@[email protected]
if @company.update_attributes(params[:company])
@[email protected]
@[email protected]refresh).size
@[email protected]refresh).size
flash[:notice] = 'Company was successfully updated.'
redirect_to :action => 'show', :id => @company
else
render :action => 'edit'
end
end
###########################
but that did'nt work.
3) Is there a Rails' way to cache the number of Employees directly in
Company.employees_count and Category.employees_count (i.e. through
a "has_many_many" relation)? That would prevent iterating through every
company of a given country/category to get its .employees_count!
Thanks a lot for your attention,
Have a good night,
Eric