# ActiveRecord calculations with joins on renamed foreign keys?

**URL:** <https://discuss.rubyonrails.org/t/activerecord-calculations-with-joins-on-renamed-foreign-keys/17918>\
**Category:** rubyonrails-talk\
**Created:** [September 8, 2007, 12:45am UTC](https://discuss.rubyonrails.org/t/activerecord-calculations-with-joins-on-renamed-foreign-keys/17918 "2007-09-08T00:45:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![J\_Y](https://avatars.discourse-cdn.com/v4/letter/j/85f322/32.png) [@J\_Y](https://discuss.rubyonrails.org/u/J_Y)\
**Post date:** [September 8, 2007, 12:45am UTC](https://discuss.rubyonrails.org/t/activerecord-calculations-with-joins-on-renamed-foreign-keys/17918/1 "2007-09-08T00:45:35Z")

</div>

Hi,

Could someone please explain to me how to do use the ActiveRecord helpers to do calculations with joins on renamed foreign keys?

My situation:

User Model: has\_many :books

Book Model: belongs\_to :created\_by, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:class\_name =\> 'User', &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:foreign\_key =\> 'created\_by'

I'm trying to calculate a list of the users by summed book scores, with the following code.

@users = User.sum('books.score', :include =\> 'books')

I'm getting the error: 42S22Unknown column 'books.user\_id' in 'on clause'. Isn't Rails supposed to be smart enough to realize that the foreign key has been renamed? How do I tell it to use created\_by as the foreign key here?

Thanks, R

---

<div class="post-metadata">

**Author:** ![Michael\_Wang](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@Michael\_Wang](https://discuss.rubyonrails.org/u/Michael_Wang)\
**Post date:** [September 8, 2007, 1:01am UTC](https://discuss.rubyonrails.org/t/activerecord-calculations-with-joins-on-renamed-foreign-keys/17918/2 "2007-09-08T01:01:14Z")

</div>

rbibby wrote:

> Hi,
> 
> Could someone please explain to me how to do use the ActiveRecord helpers to do calculations with joins on renamed foreign keys?
> 
> My situation:
> 
> User Model: has\_many :books
> 
> Book Model: belongs\_to :created\_by, &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:class\_name =\> 'User', &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:foreign\_key =\> 'created\_by'
> 
> I'm trying to calculate a list of the users by summed book scores, with the following code.
> 
> @users = User.sum('books.score', :include =\> 'books')
> 
> I'm getting the error: 42S22Unknown column 'books.user\_id' in 'on clause'. Isn't Rails supposed to be smart enough to realize that the foreign key has been renamed? How do I tell it to use created\_by as the foreign key here?

Show us your schema/migrations.

---

<div class="post-metadata">

**Author:** ![J\_Y](https://avatars.discourse-cdn.com/v4/letter/j/85f322/32.png) [@J\_Y](https://discuss.rubyonrails.org/u/J_Y)\
**Post date:** [September 8, 2007, 1:16am UTC](https://discuss.rubyonrails.org/t/activerecord-calculations-with-joins-on-renamed-foreign-keys/17918/3 "2007-09-08T01:16:25Z")

</div>

Here you go...

class CreateUsers \< ActiveRecord::Migration &nbsp;&nbsp;def self.up &nbsp;&nbsp;&nbsp;&nbsp;create\_table "users", :force =\> true do |t| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.column :login, :string &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.column :email, :string &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.column :crypted\_password, :string, :limit =\> 40 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.column :salt, :string, :limit =\> 40 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.column :created\_at, :datetime &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.column :updated\_at, :datetime &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.column :remember\_token, :string &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.column :remember\_token\_expires\_at, :datetime &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.column :activation\_code, :string, :limit =\> 40 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.column :activated\_at, :datetime &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.column :admin\_flag, :integer &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

end

class CreateBooks \< ActiveRecord::Migration

&nbsp;&nbsp;def self.up &nbsp;&nbsp;&nbsp;&nbsp;create\_table :books do |t| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.column :title, :string &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.column :description, :text &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.column :score, :integer, :default =\> 0 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.column :created\_at, :timestamp &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t.column :created\_by, :integer

&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;end

end

---

<div class="post-metadata">

**Author:** ![Michael\_Wang](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@Michael\_Wang](https://discuss.rubyonrails.org/u/Michael_Wang)\
**Post date:** [September 8, 2007, 1:26am UTC](https://discuss.rubyonrails.org/t/activerecord-calculations-with-joins-on-renamed-foreign-keys/17918/4 "2007-09-08T01:26:11Z")

</div>

What happens when you do this?:

@users = User.sum('book.score', :include =\> 'books')

---

<div class="post-metadata">

**Author:** ![J\_Y](https://avatars.discourse-cdn.com/v4/letter/j/85f322/32.png) [@J\_Y](https://discuss.rubyonrails.org/u/J_Y)\
**Post date:** [September 8, 2007, 2:11am UTC](https://discuss.rubyonrails.org/t/activerecord-calculations-with-joins-on-renamed-foreign-keys/17918/5 "2007-09-08T02:11:17Z")

</div>

Mysql::Error: #42S02Unknown table 'book' in field list: SELECT sum(book.score) AS sum\_book\_score FROM users LEFT OUTER JOIN books ON books.user\_id = users.id ORDER BY score desc

---

<div class="post-metadata">

**Author:** ![Michael\_Wang](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@Michael\_Wang](https://discuss.rubyonrails.org/u/Michael_Wang)\
**Post date:** [September 8, 2007, 3:07am UTC](https://discuss.rubyonrails.org/t/activerecord-calculations-with-joins-on-renamed-foreign-keys/17918/6 "2007-09-08T03:07:55Z")

</div>

Try it like this:

User Model: has\_many :books, :foreign\_key =\> "created\_by"

---

<div class="post-metadata">

**Author:** ![J\_Y](https://avatars.discourse-cdn.com/v4/letter/j/85f322/32.png) [@J\_Y](https://discuss.rubyonrails.org/u/J_Y)\
**Post date:** [September 8, 2007, 7:23am UTC](https://discuss.rubyonrails.org/t/activerecord-calculations-with-joins-on-renamed-foreign-keys/17918/7 "2007-09-08T07:23:30Z")

</div>

It works! Thank you so much! So is the key that the foreign key needs to be specified on both sides of the association? Is the books model being done correctly as is?

Thanks again, R

---

<div class="post-metadata">

**Author:** ![Michael\_Wang](https://avatars.discourse-cdn.com/v4/letter/m/a8b319/32.png) [@Michael\_Wang](https://discuss.rubyonrails.org/u/Michael_Wang)\
**Post date:** [September 8, 2007, 9:57am UTC](https://discuss.rubyonrails.org/t/activerecord-calculations-with-joins-on-renamed-foreign-keys/17918/8 "2007-09-08T09:57:45Z")

</div>

rbibby wrote:

> It works! Thank you so much! So is the key that the foreign key needs to be specified on both sides of the association? Is the books model being done correctly as is?
> 
> Thanks again, R

Yeah apparently it does, though the examples in AWDR don't do it that way for some reason.

For the Book model you can try:

book = Book.find(1) book.user

and it should give you back the user for that book.
