I have Country, City, Shop models
class Country < ActiveRecord::Base
has_many :cities
end
class City < ActiveRecord::Base
belongs_to :country
has_many :shops
end
class Shop < ActiveRecord::Base
belongs_to :city
end
How can I get country.shops in activerecord? (get all shops in country)
I usually use Country.cities.collect { |c| c.shops }
but this is not activerecord object.
I have considered add country_id on shop model and set has_many relation but I think it's not best way.
In Country, add a has_many :through relation:
class Country < ActiveRecord::Base
has_many :cities
has_many :shops, through: :cities
end
Now you can write country.shops and receive an appropriate ActiveRecord relation where you can say stuff like country.shops.where name:"Nieman Marcus" and other such queries.
you can def a method in Class Country
def all_shops
self.cities.collect { |c| c.shops }
end
you alse can use Mongoid::Tree
def all_shops
Shop.where(:parent_ids => self.id)
end
Related
class City < ApplicationRecord
has_many :hospitals
has_many :employees
end
class Hospital < ApplicationRecord
belongs_to :city
has_many :employees
end
class Employee < ApplicationRecord
belongs_to :hospital
belongs_to :city
end
These are my models. Is there any way to get employees for hospital where the hospital located without passing #city parameter in Hospital model.
hospital.rb
has_many :employees, ->(hospital_city) { where(city: hospital_city) }
This works fine. but I need to pass the hospital_city everytime. I want something like this.
has_many :employees, -> { #do something and return employees belongs to hospital & hospital city. }
through not works since employee table has city_id, hospital_id
You could just define a method on Hospital.
class Hospital < ApplicationRecord
belongs_to :city
has_many :employees
def employees_in_city
employees.where(city: city)
end
end
I have Following structure in my rails app.
class Country < ActiveRecord::Base
has_many :states
end
class State < ActiveRecord::Base
has_many :cities
belongs_to :country
end
class City < ActiveRecord::Base
belongs_to :state
end
I want to access to cities from the country model.
e.g. #country.cities.
Also, how can I get the country from city model?
e.g #city.country
Thanks,
Use through option in has_many and delegate for belongs_to:
class Country < ActiveRecord::Base
has_many :states
has_many :cities, through: :states
end
class State < ActiveRecord::Base
has_many :cities
belongs_to :country
end
class City < ActiveRecord::Base
belongs_to :state
delegate :country, to: :state
end
Let's say I've got:
class Town < ActiveRecord::Base
has_many :citizens
end
class Citizen < ActiveRecord::Base
belongs_to :town
has_many :cars
end
class Car < ActiveRecord::Base
belongs_to :citizen
end
Using ActiveRecord, what is the simplest way I can get a count of cars in the town?
In your models you can define a through association.
class Town < ActiveRecord::Base
has_many :citizens
has_many :cars , :through => :citizens
end
And query like this.
#town.cars.count
or
Town.find("town id").cars.count
I have the following associations:
class Venue < ActiveRecord::Base
has_many :sales
end
class Sale < ActiveRecord::Base
has_many :sale_lines
has_many :beverages, through: :sale_lines
end
class SaleLine < ActiveRecord::Base
belongs_to :sale
belongs_to :beverage
end
class Beverage < ActiveRecord::Base
has_many :sale_lines
has_many :sales, through: :sale_lines
has_many :recipes
has_many :products, through: :recipes
end
class Recipe < ActiveRecord::Base
belongs_to :beverage
belongs_to :product
end
class Product < ActiveRecord::Base
has_many :recipes
has_many :beverages, through: :recipes
end
I wan't to see the quantity of products sold by each venue, so basically I have to multiply the recipe.quantity by the sale_line.quantity of an specific product.
I would like to call #venue.calc_sales(product) to get the quantity sold of product.
Inside the class Venue I am trying to calculating it by:
class Venue < ActiveRecord::Base
has_many :sales
def calc_sales(product)
sales.joins(:sale_lines, :beverages, :recipes).where('recipes.product_id = ?', product.id).sum('sale_lines.quantity * recipe.quantity')
end
end
However, I can't access the recipes in that way.
Any idea on how to achieve it?
For the joins, you have to use a Hash to join a already-joined table. It's hard to explain, but here are some examples:
Venue.joins(:sales, :beverages) : This implies that the relations :sales and :beverages are declared on the Venue model.
Venue.joins(:sales => :beverages) : This implies that the relation :sales exists on the Venue model, and the relation :beverages exists on the Sale model.
Consider this:
Venue
has_one :sale
Venue.joins(:sales) : This would not work, you have to use the exact same name as the relation between the Venue model & Sale model.
Venue.joins(:sale) : This would work because you used the same name of the relation.
Attention: You have to use the pluralized name in the where clause:
Venue.joins(:sale).where(:sales => { :id => sale.id })
^^ ^^ # See the plural
In your case, you can do something like this:
sales.joins(:sale_lines => { :beverage => :recipes })
.where(:recipes => { :product_id => product.id })
.sum('sale_lines.quantity * recipes.quantity')
Im setting up a reminder service that sends deals via email in relation to a persons interests AND city.. Basically, the user inputs important dates (friends bday, anniversary ect) and the interests of that special person.
I want to send them deals based on 1)the users city and 2)the interests of the related person
How should i setup my associations for the Deal model?
What i have so far..
class User < ActiveRecord::Base
belongs_to :city
has_many :person_interests, :as => :person
has_many :interests, :through => :person_interests
end
class City < ActiveRecord::Base
attr_accessible :name
belongs_to :province
has_many :users
end
class PersonInterest < ActiveRecord::Base
belongs_to :interest
belongs_to :person, :polymorphic => true
end
class Interest < ActiveRecord::Base
has_many :person_interests
end
Thanks!
If a deal could apply to more than one interest, you'd start with something like:
class Deal < ActiveRecord::Base
belongs_to :interests
belongs_to :city
end
class City < ActiveRecord::Base
attr_accessible :name
belongs_to :province
has_many :users
has_many :deals
end
class Interest < ActiveRecord::Base
has_many :person_interests
has_many :deals
end
And then you could do something like
#relevant_deals = #city.deals.where(:interest_id => 'abc')
or
#relevant_deals = #interest.deals.where(:city_id => 'def')