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?

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Jason Fleetwood-Boldt at Sep 28, 2011 at 7:46 pm

    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+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • ERB at Sep 28, 2011 at 8:02 pm
    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 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+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • ERB at Sep 28, 2011 at 8:03 pm
    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:46 pm, Jason Fleetwood-Boldt 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+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • ERB at Sep 28, 2011 at 8:06 pm
    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:46 pm, Jason Fleetwood-Boldt 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+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Robert Walker at Sep 28, 2011 at 10:05 pm

    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:

    http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

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

    --
    Posted via http://www.ruby-forum.com/.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Michael Pavling at Sep 29, 2011 at 5:24 am

    On 28 September 2011 20:46, Jason Fleetwood-Boldt 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.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Michael Pavling at Sep 29, 2011 at 5:25 am

    On 28 September 2011 20:28, 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
    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.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Craig White at Oct 1, 2011 at 2:38 pm

    On Sep 28, 2011, at 12: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
    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?
    ----
    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.

    http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

    Craig

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedSep 28, '11 at 7:28p
activeOct 1, '11 at 2:38p
posts9
users5
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2022 Grokbase