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:46 pm, Jason Fleetwood-Boldt <t...@datatravels.com>
wrote:
> 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
>
> > 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')
>
> 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.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to