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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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: userthis 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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...-eJ8lBn5LdNBhbmWW9KSYcQ@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
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 <t...-eJ8lBn5LdNBhbmWW9KSYcQ@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
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 <t...-eJ8lBn5LdNBhbmWW9KSYcQ@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 28 September 2011 20:46, Jason Fleetwood-Boldt <tech-eJ8lBn5LdNBhbmWW9KSYcQ@public.gmane.org> wrote:> On Sep 28, 2011, at 3:28 PM, ERB wrote: > class Company < ActiveRecord::Base > belongs_to: user > > > this should be > belong_to :usererm. 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
On 28 September 2011 20:28, ERB <rpreville-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > endIf 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.