Multiple Associations to Same Model - ruby-on-rails

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

Related

Multi level joins in rails 4

I want to make this query in rails 4
select r.region_id, r.region_name from countries c, zones z, regions r where c.country_id = $country_id (pass as parameter) and c.country_id = z.zone_id and z.subzone_id = r.region_id
Models :
#Country.rb
class Country < ActiveRecord::Base
has_one :place, foreign_key: :place_id
has_many :zones , foreign_key: :place_id
has_many :subzones, :through => :zones
end
#Zone.rb
class Zone < ActiveRecord::Base
belongs_to :place
belongs_to :subzone, :class_name => 'Place' , :foreign_key => :subzone_id
end
#Region.rb
class Region < ActiveRecord::Base
has_one :place , foreign_key: :place_id
end
#Place.rb
class Place < ActiveRecord::Base
belongs_to :region, :foreign_key => :place_id
has_many :zones, :foreign_key => :place_id
has_many :subzones, :through => :zones
end
I have tried this:
Country.joins(:zones).where("zone.subzone_id = regions.region_id AND country_id = ?",$country_id )
but got error as :
Java::JavaSql::SQLSyntaxErrorException: ORA-00904: "REGIONS"."REGION_ID": invalid identifier.....
Not sure how to load region in above query...
Thanks in advance :-)
Region.joins(zones: :country).where(country: {country_id: $country_id})
This will only work if you have your models like this:
#Country.rb
class Country < ActiveRecord::Base
has_many :zones, as: :zone
end
#Zone.rb
class Zone < ActiveRecord::Base
has_many :regions, as: :region
belongs_to :country, foreign_key: :zone_id
end
#Region.rb
class Region < ActiveRecord::Base
belongs_to :zone, foreign_key: :region_id
end
when using string for the where statement after a join or include, you need to use the table name, not the relation name to refer to, so where you have written
zone.subzone_id
you would need to write
zones.subzone_id
Your models and relations are a bit confusing and its hard to see what your trying to achieve relation wise with them, place_id seems to be being used as a reference for many things that have nothing to do with the Place class, and it could get a bit messy in the future.
However from what I can grasp, in terms of multi level joins, something like:
Country.joins(zones: {place: :region}).where.........
is something along the lines of what you want to set up your table joins for your query. Play around with that or things similar, hope it helps.

Rails 3 / Active Record - Association problems with additional model in assignment table

I currently have a setup that links the models User, Dealer and Role together. User and Dealer is many to many, and is working as expected with a Dealer_user assignment table.
The problem is that I want to have roles assigned to the user that are specific to the dealer also (i.e. a user could be a Sales Manager and a Parts Manager in one dealership, while being a Sales Manager and a Director in another).
In order to do this, I have a Role model (which belongs to a Role_type). Role should belong to Dealer_user, and Dealer_user has many Roles.
The intention is that I will be able to do dealer.users.where(:id => user.id).first.roles and it will return only the roles specific to that dealership.
The problem I have is that when I run the following test code: dealer.users.where(:id => user.id).first.roles.create(:role_type_id => 1 + Random.rand(4))
I get an error: Cannot modify association 'User#roles' because the source reflection class 'Role' is associated to 'DealerUser' via :has_many.
Can anyone suggest what I am doing wrong with my models (which are below)?
NOTE: The belongs_to relationship that Role has with Dealer_user is polymorphic because it could also belong to Sale_user or other association tables, which require the same functionality as Dealer.
class Dealer < ActiveRecord::Base
attr_accessible :name, :address_id
has_many :dealer_users
has_many :users, :through => :dealer_users
has_many :roles, :through => :dealer_users
end
class User < ActiveRecord::Base
attr_accessible :first_name, :last_name
has_many :dealer_users
has_many :dealers, :through => :dealer_users
has_many :roles, :through => :dealer_users
end
class DealerUser < ActiveRecord::Base
attr_accessible :dealer_id, :user_id
belongs_to :dealer
belongs_to :user
has_many :roles, :as => :role_originator
end
class Role < ActiveRecord::Base
attr_accessible :role_type_id
belongs_to :role_type
belongs_to :role_originator, :polymorphic => true
end
Edit: No luck so far - can anyone help?
I would use has_many through association with the roles table. Get rid of the dealer_user table, and add columns to the roles table dealer_id and user_id
Then your models would look something like this:
class Dealer < ActiveRecord::Base
has_many :users, :through => :roles
has_many :roles
end
class User < ActiveRecord::Base
has_many :dealers, :through => :roles
has_many :roles
end
class Role < ActiveRecord::Base
belongs_to :dealer
belongs_to :user
end
That should make it easier to do the types of queries you're trying. The rails guide has a really good overview here

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.

ActiveRecord has two association

What is the best way to achieve a has two association with activerecord?
I have a Team and Game models. Each Team will have_many games #team.games. A Game will have two teams #game.hosting_team and #game.opposing_team.
I started out with two belongs_to/has_one associations but then #team.games would only return their home games.
The other option I can think of is using a HABTM and use a validator to ensure there are only records. The only thing missing is keeping track of the home team. It seems like I need a has many through association but I'm not exactly sure...
Thanks for your help.
This is an example of how the two has_many associations look. The problem here is I would have to call team.games and team.opponents to get a full list of their games
class Team < ActiveRecord::Base
has_many :games
has_many :opponents, :class_name => "Team"#, :foreign_key => ""
end
class Game < ActiveRecord::Base
belongs_to :team, :class_name => "Team" #, :foreign_key => "team_id"
belongs_to :opponent, :class_name => "Team" #, :foreign_key => "opponent_id"
end
I'd like something like this but this obviously isn't how belongs_to works.
class Team < ActiveRecord::Base
has_many :games
end
class Game < ActiveRecord::Base
belongs_to :hosting_team
belongs_to :opposing_team
end
My desired api would look like this.
#team.games # return all games home or away
#game.hosting_team # Team
#game.opposing_team # Team
You can probably still model it with the bt/ho associations, and set up games as an accessor method on the team instead of as an association:
class Team < ActiveRecord::Base
def games
Game.find(:conditions => ["home_team_id = ? OR away_team_id = ?", id, id])
end
end

Relationship like Twitter followers/followed in ActiveRecord

I'm trying to represent a relationship between users in my application where a user can have many followers and can follow other users.
I would like to have something like user.followers() and user.followed_by()
Could you please tell me in details how to represent this using ActiveRecord?
Thanks.
You need two models, a Person and a Followings
rails generate model Person name:string
rails generate model Followings person_id:integer follower_id:integer blocked:boolean
and the following code in the models
class Person < ActiveRecord::Base
has_many :followers, :class_name => 'Followings', :foreign_key => 'person_id'
has_many :following, :class_name => 'Followings', :foreign_key => 'follower_id'
end
and corresponding in the Followings class you write
class Followings < ActiveRecord::Base
belongs_to :person
belongs_to :follower, :class_name => 'Person'
end
You could make the names clearer to your liking (i especially don't like the Followings-name), but this should get you started.
The twitter follower model differs from the friendship model in that you do not need permission from the person to follow them. Here I set up a lazy loading where the relationship is not fully defined in the person object.
/app/models/person.rb
def followers
Followership.where(:leader_id=>self.id).not_blocked
end
def following
Followership.where(:follower_id=>:self.id).not_blocked
end
has_many :followers, :class_name=>'Followership'
has_many :followed_by, :class_name=>'Followership'
/app/models/followership.rb
belongs_to :leader, :class_name=>'Person'
belongs_to :follower, :class_name=>'Person'
#leader_id integer
#follower_id integer
#blocked boolean
scope :not_blocked, :conditions => ['blocked = ?', false]

Resources