Rails 4: Show List of Items in third level associations - ruby-on-rails

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

Related

rails association belongs_to

I know sty must be wrong in the way I build my db but please take a minute answering this : I am building a supermarket model, where 1 user has a shopping list, each list has many products.
So what I do is :
class User < ActiveRecord::Base
has_many :lists
end
class List < ActiveRecord::Base
belongs_to :user
has_many :products
end
class Product < ActiveRecord::Base
????
end
A list has several products but products don't belong to lists. What should I do to have Users having many lists, and lists having many products ?
regards,
Have a class that links them via has_many through.
class ListItem < ActiveRecord::Base
belongs_to :list
belongs_to :product
end
class List < ActiveRecord::Base
belongs_to :user
has_many :list_items
has_many :products, through: :list_items
end
You don't need an additional class. Rails can manage this for you with the has_and_belongs_to_many_association
In your case, it would be:
class User < ActiveRecord::Base
has_many :lists
end
class List < ActiveRecord::Base
belongs_to :user
has_and_belongs_to_many :products
end
class Product < ActiveRecord::Base
has_and_belongs_to_many :lists
end
Of course, you need to add the join table in the migration:
create_table :lists_products, id: false do |t|
t.belongs_to :list
t.belongs_to :product
end

has_many and has_many relation

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

Active Record, count of grandchild association record

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

Proper Rails association setup

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')

Model associations for a contest

I'm new to rails and working on an app that has the following situation:
Users have skills (e.g rafting, dancing)
Users participate in contests
Contest measures multiple skills
At the end of each contest, each user gets a score (e.g dancing: 5, rafting: 4)
Whats the best way to model this ?
Thanks,
This got nasty :s At the end I was actually not sure if this is the right way
class Skill < ActiveRecord::Base
has_many :skill_scores
has_many :user_skills
end
class UserSkill < ActiveRecord::Base
belongs_to :user
belongs_to :skill
end
class SkillScore < ActiveRecord::Base
belongs_to :user
belongs_to :contest
belongs_to :skill
end
class User < ActiveRecord::Base
has_many :skills
has_many :contests, :through => :contest_participations
has_many :skill_scores
end
class Contest < ActiveRecord::Base
has_many :users, :through => :contest_participations
has_many :skill_scores
end
class ContestParticipation < ActiveRecord::Base
belongs_to :user
belongs_to :contest
end

Resources