-
Notifications
You must be signed in to change notification settings - Fork 438
Description
Model User
has_many projects, through project_users
Model ProjectUser
belongs_to project
belongs_to user
Model Project
has_many users, through project_users
Attempted code:
$user = User:find(1)
$user->projects ** Gives me the error:
Uncaught exception 'ActiveRecord\HasManyThroughAssociationException' with message 'Could not find the association project_users in model User'
So I change User model to:
Model User
has_many project_users
has_many projects, through project_users
And I get the same exception complaining about a problem in the User model...
The problem was of course:
Model Project
has_many users, through project_users
has_many project_users ** missing **
The full trace is:
Fatal error: Uncaught exception 'ActiveRecord\HasManyThroughAssociationException' with message 'Could not find the association project_users in model User' in /usr/share/php/php-activerecord/lib/Relationship.php:464
Stack trace:
#0 /usr/share/php/php-activerecord/lib/Model.php(493): ActiveRecord\HasMany->load(Object(User))
#1 /usr/share/php/php-activerecord/lib/Model.php(386): ActiveRecord\Model>read_attribute('projects')
#2 /home/mdm162/www/ntc/controllers/Timecard.controller.php(85): ActiveRecord\Model->__get('projects')
#3 /home/mdm162/www/ntc/controllers/Timecard.controller.php(60): TimecardController->printTimecard(Object(Timecard))
#4 /home/mdm162/www/ntc/controllers/Timecard.controller.php(27): TimecardController->loadTimecard()
#5 /home/mdm162/www/ntc/timecard.php(20): TimecardController->handleAction()
#6 {main} thrown in /usr/share/php/php-activerecord/lib/Relationship.php on line 464
The offending lines I believe are:
http://github.com/kla/php-activerecord/blob/master/lib/Relationship.php
Line 461
// verify through is a belongs_to or has_many for access of keys
if (!($through_relationship = $this->get_table()->get_relationship($this->through)))
throw new HasManyThroughAssociationException("Could not find the association $this->through in model " . get_class($model));
I have noticed similar behavior in exceptions when modifying has_many through with both 'class_name' and 'source'.
The modifier 'source' is not mentioned once on this documentation page:
http://www.phpactiverecord.org/projects/main/wiki/Associations