Query through associations - Rails 3 - ruby-on-rails

I'd like to make some queries just like this:
employees = Employee.where(:group.name => 'admin')
employees = Employee.where(:company.address.city => 'Porto Alegre')
I mean, I need to access fields that are in another model via association.
Thanks in advance

assuming a company can have multiple addresses (which I'm assuming because of your company.address.city namespacing, and because it makes for an interesting query example):
class Group < ActiveRecord::Base
has_many :employees
end
class Employee < ActiveRecord::Base
belongs_to :group
belongs_to :company
end
class Company < ActiveRecord::Base
has_many :employees
has_many :addresses
end
class Address < ActiveRecord::Base
belongs_to :company
end
The queries you're looking for would be as follows:
Employee.
joins(:group).
where(:groups => { :name => 'admin' })
Employee.
joins(:company => :addresses).
where(:addresses => { :city => 'Porto Alegre' })
note that in the where clauses above the plural form of the association is always used. The keys in the where clauses refer to table names, not the association name.

Related

Optimize ActiveRecord query

I have 2 tables in database: cars and domains.
One car can have many domains and one domain can have many cars.
In my project three models:
class Car < ActiveRecord::Base
has_many :cars_domains
has_many :domains, :through => :cars_domains
...
class Domain < ActiveRecord::Base
has_many :cars_domains
has_many :cars, :through => :cars_domains
...
class CarsDomain < ActiveRecord::Base
belongs_to :car
belongs_to :domain
end
I want to see cars which without domain:
#cars = Car.find(:all, :conditions => ['id not in(select car_id from cars_domains where domain_id = ?)', params[:domain_id]])
It's work, but I think it's very difficult. Maybe possible to do it more simple?
Tried this in a quick app I created:
domain = Domain.find(params[:domain_id])
Car.includes(:cars_domain).where('cars_domain.domain_id <> ?', domain.id)
The reason I query the domain object is because I'm always weary about passing a value from the request headers as a query parameter in SQL.

Rails - Query with has_many association

I am currently trying to create a custom method on a model, where the conditions used are those of a has_many association. The method I have so far is:
class Dealer < ActiveRecord::Base
has_many :purchases
def inventory
inventory = Vehicle.where(:purchases => self.purchases)
return inventory
end
end
This is not working, due to the fact that Vehicle has_many :purchases (thus there is no column "purchases" on the vehicle model). How can I use the vehicle.purchases array as a condition in this kind of query?
To complicate matters, the has_many is also polymorphic, so I can not simply use a .join(:purchases) element on the query, as there is no VehiclePurchase model.
EDIT: For clarity, the relevant parts of my purchase model and vehicle models are below:
class Purchase < ActiveRecord::Base
attr_accessible :dealer_id, :purchase_type_id
belongs_to :purchase_item_type, :polymorphic => true
end
class Vehicle < ActiveRecord::Base
has_many :purchases, :as => :purchase_item_type
end
class Dealer < ActiveRecord::Base
def inventory
Vehicle.where(:id => purchases.where(:purchase_item_type_type => "Vehicle").map(&:purchase_item_type_id))
end
end
Or:
def inventory
purchases.includes(:purchase_item_type).where(:purchase_item_type_type => "Vehicle").map(&:purchase_item_type)
end
I was able to do this using the :source and :source_type options on the Vehicle model, which allows polymorphic parents to be associated.

How to handle 2 columns which refer same master table with ActiveRecord on Rails3

When one table has 2 columns which refer same master table with ActiveRecord on Rails3
There are tables like below.
Depts
id
dept_name
Users
id
dept_id
previous_dept_id
Users table has two columns which refer Depts table.
How can I get dept_name for each columns?
class Dept < ActiveRecord::Base
has_many :user
end
class User < ActiveRecord::Base
belongs_to :dept
end
I think what you're looking for is the following:
class User < ActiveRecord::Base
belongs_to :dept
belongs_to :previous_dept, :class_name => 'Dept', :foreign_key => 'previous_dept_id'
end
You should then be able to access the two departments like this:
dept_name = user.dept.dept_name
previous_dept_name = user.previous_dept.dept_name
It's important to note that your Dept model will only find users by the dept_id column. I think you'd have to add a second has_many to Dept if you needed to find users by their previous dept. Something like:
class Dept < ActiveRecord::Base
has_many :user
has_many :previous_user, :class_name => 'User', :foreign_key => 'previous_dept_id'
end

rails join of a join (rails 2.3.5 and ruby 1.8.7)

I have
data_records, brands and influencers
data_records have many brands
data_records have 1 influencer
brands have many influencers via brand_influencers association which has an attribute called top (boolean).
here are my models:
class DataRecord < ActiveRecord::Base
belongs_to :influencer
has_and_belongs_to_many :brands
end
class Brand < ActiveRecord::Base
has_and_belongs_to_many :data_records
end
class Influencer < ActiveRecord::Base
has_many :brands_influencers
has_many :brands, :through => :brands_influencers
end
class BrandsInfluencer < ActiveRecord::Base
belongs_to :brand
belongs_to :influencer
end
I would like to do one query to get all data_records for a given brand where the influencers are in the brands top influencers (top = true).
I need to start on the data_record model because there are other dynamic queries that can be bolted on to this query (this is a typical big filter type screen).
So my question, is it possible to join in a realtionship of a join. I have used joins brands and it works fine, but I cant figure out a way to join in the brand_influencers relationship
thanks
Joel
The rails 2.3.5 version of it is,
DataRecord.find :all, :conditions => ['brands.name = ? AND influencers.top = ?', 'name', true], :joins => {:brands => :influencers}
I am supposing there's a name for a brand and you're looking for the brand by name. And you need to change Brand like this:
class Brand < ActiveRecord::Base
has_and_belongs_to_many :data_records
has_and_belongs_to_many :influencers
end
Sounds like you want to use
has_many :foos, :through=>:bar
or
has_one :foo, :through=>:bar
Try this:
class DataRecord < ActiveRecord::Base
# how does this relate to the other models?
end
class Brand < ActiveRecord::Base
has_and_belongs_to_many :influencers
end
class Influencer < ActiveRecord::Base
has_and_belongs_to_many :brands
end
class BrandsInfluencers < ActiveRecord::Base
# notice the name change in this class (pluralization)
belongs_to :brand
belongs_to :influencer
end
You can get a join from another and reference them in a where like this:
DataRecord.joins(:influencers => {:brand_influencers}, :brands).
where(:brands => {:id => 123}).
where(:brands => {:brand_influencers => {:top => true}})
I'm pretty sure it works similarly in 2.3.x. Try this:
DataRecord.joins(:influencers => {:brand_influencers}, :brands).
conditions(:brands => {:id => 123}).
conditions(:brands => {:brand_influencers => {:top => true}})
I would suggest you build up the relation bit by bit and check the SQL that's being generated. Good luck!

Multiple Associations to Same Model

I have two classes that I would like to specify as follows:
class Club < ActiveRecord::Base
belongs_to :president, :class_name => "Person", :foreign_key => "president_id"
belongs_to :vice_president,
:class_name => "Person",
:foreign_key => "vice_president_id"
end
class Person < ActiveRecord::Base
has_one :club, :conditions =>
['president_id = ? OR vice_president_id = ?', '#{self.id}', '#{self.id}']
end
This doesn't work and gives me an error when trying to get the club association from the person object. The error is because is looking for person_id in the club table when I looked at the SQL. I can get around it by declaring multiple has_one associations, but feel like this is the improper way of doing it.
A person can only be the President or Vice President of one club.
Anyone able to offer a little bit of advice on this issue, I would be very appreciative.
Your has_one condition will never work in Rails, as far as I know.
You need one explicit has_one or belongs_to or has_many per "link", on both tables. So if you have two "links", you need two has_one and two belongs_to. That is how it works.
Secondly, I think you should reconsider your models. The way you are doing it, one person can not be the president of a club and an employee, at the same time. Or be the president of two clubs. Even if you don't have these right now, they can come in the future - it is easier to stay flexible right now.
A flexible way of doing this is using a has_many :through with an intermediate table that specifies the role. In other words:
# The memberships table has a person_id, club_id and role_id, all integers
class Membership < ActiveRecord::Base
belongs_to :club
belongs_to :person
validates_presence_of :role_id
validates_numericality_of :role_id
end
class Club < ActiveRecord::Base
has_many :memberships, :dependent => :delete_all
has_many :people, :through => :memberships
end
class Person < ActiveRecord::Base
has_many :memberships, :dependent => :delete_all
has_many :clubs, :through => :memberships
end
Now, assuming that role_id=0 means employee, role_id=1 means president, and role_id=2 means vice_president, you can use it like this:
tyler = Person.find(1) # person_id is 1
other = Person.find(2) # person_id is 2
c = Club.find(1) # club_id is 1
tyler.clubs # returns all the clubs this person is "member" of
c.people # returns all the "members" of this club, no matter their role
#make tyler the president of c
tyler.memberships.create(:club_id => 1, :role_id => 1)
#make other the vicepresident of c
#but using c.memberships instead of other.memberships (works both ways)
c.memberships.create(:person_id => 2, :role_id => 1)
#find the (first) president of c
c.memberships.find_by_role_id(1).person
#find the (first) vicepresident of c
c.memberships.find_by_role_id(2).person
#find all the employees of c
c.memberships.find_all_by_role_id(0).collect { |m| m.person }
#find all the clubs of which tyler is president
tyler.memberships.find_all_by_role_id(1).collect { |m| m.club }
Additional notes:
You could complement this with a roles table and model. Roles would have just a a name, roles would have_many relationships and memberships would belong_to role. Or, you could define methods in memberships for getting the role name (if 0, it returns "employee", if 1, "president", etc
You can add validations on memberhips so no more than 1 person can be made president of a given club, or the same employee on the same club twice. Later on, if you start getting "exceptional cases" in which a person needs to be in two places, you will just have to adapt your validations.
I think your associations are the wrong way around. Your way it is hard to assign a president or vice president.
I would do it like this:
class Club < ActiveRecord::Base
has_one :president,
:class_name => "Person",
:foreign_key => 'president_club_id'
has_one :vice_president,
:class_name => "Person",
:foreign_key => 'vice_president_club_id'
end
class Person < ActiveRecord::Base
belongs_to :club
end
Now you can assign the roles like this:
club.president = Person.create(:name => 'Tom')
club.vice_president = Person.create(:name => 'Andrew')
I suggest you introduce a new model called role. Then have the following:
class Club
has_many :roles
def president
end
def vice_president
end
end
class Person
belongs_to :role
end
class Role
has_one :person
belongs_to :club
end
This is the classic use case for polymorphic associations.
Here is the link:
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
Something like..
class Club < ActiveRecord::Base
belongs_to :person, :polymorphic => true
class Person < ActiveRecord::Base
has_one :club, :as => :position

Resources