Newbie trying to understand include

I have two models, users and companies as indicated below. The users
table has a foreign_key “company_id”.

class User < ActiveRecord::Base
has_many :companies
end

class Company < ActiveRecord::Base
belongs_to: user
end

In my controller:

@user = User.where(:id => params[:id]).includes(:companies)

I get the following error:
SQlite3::SQLException: no such column: companies.user_id: SELECT
“companies”.* FROM “companies” WHERE “companies”.“user_id” IN (‘8’)

Why is RAILS getting the foreign key wrong and why is it trying to
pull columns in the users table into the companies model?

On Sep 28, 2011, at 3:28 PM, ERB wrote:

I have two models, users and companies as indicated below. The users
table has a foreign_key “company_id”.

class User < ActiveRecord::Base
has_many :companies
end

class Company < ActiveRecord::Base
belongs_to: user

this should be

belong_to :user

Did you create a migration and run rake db:migrate ?

Why is RAILS getting the foreign key wrong and why is it trying to
pull columns in the users table into the companies model?

Because you used .includes it is trying to eager load the companies. If
you don’t know what eager loading is, you don’t need it right now. One
day you will learn what eager loading is and you will then learn to use
it, but until that day you can get by without it.

If you do know what eager loading is and your intention was to use it
here, that’s a different matter.

FYI: I kinda of get the concept of eager loading and thought it would
be better than the alternative… is there something else I need to do
in order to get eager loading to work?

On Sep 28, 3:46pm, Jason Fleetwood-Boldt [email protected]

I did create the migration and the tables exist in the db.

If I use .joins, I have a similar error as in:

@user = User.where(:id => params[:id] ).joins(:companies)

SQLite3::SQLException: no such column: companies.user_id: SELECT
“users”.* FROM “users” INNER JOIN “companies” ON “companies”.“user_id”
= “users”.“id” WHERE “users”.“id” = 8

It’s like something is wrong with the model associations… do I have
to specify the foreign_key? It should conform to the convention.

On Sep 28, 3:46pm, Jason Fleetwood-Boldt [email protected]

this works for me:
@user = User.where(:id => params[:id] ).joins(‘LEFT JOIN companies
ON companies.id = users.company_id’)

so then I guess its a question on understanding eager loading?

On Sep 28, 3:46pm, Jason Fleetwood-Boldt [email protected]

ERB wrote in post #1024149:

this works for me:
@user = User.where(:id => params[:id] ).joins(‘LEFT JOIN companies
ON companies.id = users.company_id’)

so then I guess its a question on understanding eager loading?

Think carefully about what you’re trying to solve here. Eager loading is
intended to prevent the n+1 query problem.

In the case you show here your eager loading would actually cause more
queries than not using eager loading at all. You are trying to find a
single user (1 SQL query), then you’re trying to ask for eager loading
(1+1 SQL queries). Not only have you not saved yourself anything, you
actually reduced performance.

By fetching only one user object you might as well wait until some
information for the associated companies are actually needed before
hitting the database.

Take a look at the first example in the section on Eager Loading that
you’ll find a little less than half way down on the following page:

There is a very clear explanation of what eager loading is, and how it
should be used.

On 28 September 2011 20:28, ERB [email protected] wrote:

I have two models, users and companies as indicated below. The users
table has a foreign_key “company_id”.

class User < ActiveRecord::Base
has_many :companies
end

class Company < ActiveRecord::Base
belongs_to: user
end

If you user has many companies, then Company needs the foreign key
user_id to belong to user.
Change your DB schema; remove the company_id field from users and add
the field to companies.

On 28 September 2011 20:46, Jason Fleetwood-Boldt [email protected]
wrote:

On Sep 28, 2011, at 3:28 PM, ERB wrote:
class Company < ActiveRecord::Base
belongs_to: user

this should be
belong_to :user

erm. No it shouldn’t. “belongs_to” is the correct call.

On Sep 28, 2011, at 12:28 PM, ERB wrote:

pull columns in the users table into the companies model?


class Company < AR::B
belongs_to :user

that’s why

also, if
class User < AR::B
has_many :companies

then having a column named users.company_id is a waste of time and space
too.

Craig