Trouble with simple Rails nested associations where clause for parent

rails associations
rails associations tutorial
rails nested attributes
rails associations examples
rails nested resources controller
rails has_many foreign key
rails inverse_of
rails many-to-many

I have the following models:

class Business < ApplicationRecord
  has_many :shopping_trips
end

class ShoppingTrip < ApplicationRecord
  belongs_to :business
  has_many :purchases
end

class Purchase < ApplicationRecord
  belongs_to :shopping_trip
end

So a Business can have many shopping trips, and each of these shopping trips can have many purchases.

I am trying to run a simple query on the Purchase table to find purchases that belong to a particular business. So I'm writing this:

purchases = Purchase.joins(:shopping_trip => :business ).where(:shopping_trip => {:business_id => 1})

Unfortunately it's not working. I get the following error:

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "shopping_trip"
LINE 1: ...sses"."id" = "shopping_trips"."business_id" WHERE "shopping_...
                                                             ^
: SELECT "purchases".* FROM "purchases" INNER JOIN "shopping_trips" ON "shopping_trips"."id" = "purchases"."shopping_trip_id" INNER JOIN "businesses" ON "businesses"."id" = "shopping_trips"."business_id" WHERE "shopping_trip"."business_id" = $1

The join looks about right but the where clause seems to fail.


ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: missing FROM-clause entry for table "shopping_trip"

You need to specify table name not the association name inside the where. So shopping_trip should be shopping_trips

purchases = Purchase.joins(:shopping_trip => :business ).where(:shopping_trips => {:business_id => 1})

Active Record Associations, I have the following models: class Business < ApplicationRecord has_many :​shopping_trips end class ShoppingTrip < ApplicationRecord belongs_to :​business  Step 2 — Specifying Nested Routes and Associations for the Parent Model Rails has already set the belongs_to association in our Post model, thanks to the :references keyword in the rails generate scaffold command, but in order for that relationship to function properly we will need to specify a has_many association in our Shark model as well.


A better solution is to set up indirect associations so that you can query through the join model without manually joining:

class Business < ApplicationRecord
  has_many :shopping_trips
  has_many :purchases, through: :shopping_trips
end

class ShoppingTrip < ApplicationRecord
  belongs_to :business
  has_many :purchases
end

class Purchase < ApplicationRecord
  belongs_to :shopping_trip
  has_one :business, through: :shopping_trip
end

You can now query from either side:

@business = Business.eager_load(:purchases).find(1)
@purchases = @business.purchases

# or
@purchases = Purchase.eager_load(:buisness).where(businesses: { id: 1 })

Define a context for a nested association · Issue #1554 · rails-api , How to declare associations between Active Record models. is also useful for setting up "shortcuts" through nested has_many associations. If you have an instance of the Picture model, you can get to its parent via @picture.imageable . The select method lets you override the SQL SELECT clause that is used to  Definitely broken AR associations, Travis was green and the only change in the PR was the Rails upgrade from 3.2.5 to 3.2.6. This comment has been minimized. Sign in to view


Check This...

all_purchase = Purchase.all

all_purchase.each do |each_purchase| each_purchase.shopping_trip.business end

Issue #37701 · rails/rails, Define a context for a nested association #1554 embedded within a parent serializer), an if statement would be required to check whether or not it Hopefully I haven't missed something simple that would let me do this grin  Nested Attributes is a feature that allows you to save attributes of a record through its associated parent. In this example we’ll consider the following scenario: We’re making an online store with lots of products. Each Product can have zero or more Variants.


How To Create Nested Resources for a Ruby on Rails Web App , ActiveStorage changes in nested model not detected during update #37701 And updating the Child model through a very vanilla Parent's controller: a repo containing a simple rails app that demonstrates the problem, thanks. First thing I thought was the associations that Active Storage creates aren't  In Rails, an association is a connection between two Active Record models. Why do we need associations between models? Because they make common operations simpler and easier in your code. For example, consider a simple Rails application that includes a model for authors and a model for books. Each author can have many books.


Using scopes with has_many and belongs_to, Ruby on Rails is a web application framework written in Ruby that offers developers an Step 2 — Specifying Nested Routes and Associations for the Parent Model. Rails In this case, the only change we will need to make is to the redirect_to statement, to handle successful updates. 2. Report an issue  That’s followed by some tips and tricks for working with associations, and then by a complete reference to the methods and options for associations in Rails. 2 The Types of Associations. In Rails, an association is a connection between two Active Record models. Associations are implemented using macro-style calls, so that you can declaratively add features to your models.


Ruby on Rails Nested Attributes | Pluralsight, Implementing scopes on your associations can cause much confusion and which start with PG::UndefinedTable: ERROR: missing FROM-clause entry for table with a quick reference on using scopes with has_many and belongs_to in Rails. scopes in the parent model which reference attributes of your association. 1 Why Associations? In Rails, an association is a connection between two Active Record models. Why do we need associations between models? Because they make common operations simpler and easier in your code. For example, consider a simple Rails application that includes a model for authors and a model for books. Each author can have many books.