Optimize ActiveRecord query - ruby-on-rails

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.

Related

Query through associations - Rails 3

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.

Ruby on rails querying with join and condition statement

I need to query joining tables without using the primary key(id).
class User < ActiveRecord::Base
has_one :participant
end
class Participant < ActiveRecord::Base
belongs_to :user
end
The User has 'id' and 'name'.
The Participant has 'user_id' which is User.id
I am trying to find the Participant.id by querying with User.name
What I have tried is,
participant_id = Participant.all :joins => :users, :conditions => {:name => "Susie"}
If you're just looking for a specific user's participant id, you could just go:
User.find_by_name("Susie").participant.id
Since your user has a has_one relation to Participant(which belongs to it -- so basically a one-to-one) you can just go call participant on user. ActiveRecord takes care of the join magicks for you
Try the following:
class User < ActiveRecord::Base
has_one :participant
end
class Participant < ActiveRecord::Base
belongs_to :user
scope :foo, lambda {|name|
joins(:users).
where("name = ?", name)
}
end
If you're taking user input you're going to want to probably fix joins so it uses sanitize_sql_array. See here also for some query goodness

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!

Using default_scope in a model, to filter by the correct instanceID

I have two tables:
books (id, name, desc, instance_id)
instances (id, domain)
A user should ONLY be able to see data that is assigned to their instance_id in records...
For the books, model, to accomplish this, I'm thinking about using a default scope.. Something like:
class Books < ActiveRecord::Base
attr_accessible :name, :description
belongs_to :user
default_scope :order => 'books.created_at DESC'
AND books.instance_id == current.user.instance_id
end
Any thoughts on that idea? Also how can I write that 2nd to last line for Rails 3? 'AND books.instance_id == current.user.instance_id'
Thanks
It's not a good idea to access the current user inside the model. I would implement this as follows:
class Instance < ActiveRecord::Base
has_many :users
has_many :books
end
class User < ActiveRecord::Base
belongs_to :instance
has_many :books, :order => "created_at DESC"
has_many :instance_books, :through => :instance, :source => :books,
:order => "created_at DESC"
end
class Book < ActiveRecord::Base
belongs_to :user
belongs_to :instance
end
List of Books associated with the user instance:
current_user.instance_books
List of Books created by the user:
current_user.books
Creating a new book:
current_user.books.create(:instance => current_user.instance, ..)
Note:
Your book creation syntax is wrong. The build method takes hash as parameter. You are passing two arguments instead of one.
user.books.build(params[:book].merge(:instance => current_user.instance}))
OR
user.books.build(params[:book].merge(:instance_id => current_user.instance_id}))

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