Trouble locating and displaying list of records from a relationship model in Rails app

前端 未结 3 580
终归单人心
终归单人心 2021-01-15 01:16

I have a relationship model that tracks customers (users) of shops in a has_many :through association. I\'m having trouble with my ShopsController

相关标签:
3条回答
  • 2021-01-15 01:47

    ... for a given shop ...

    This suggests that you should be finding an instance of Shop, and from it using associations to retrieve the appropriate Users. I'd expect to see this in a shop-oriented view, not a relationship view, and for there to be an association on Shop of:

    has_many :users, through => :relationships
    
    0 讨论(0)
  • 2021-01-15 02:02

    If I understand your use case, a shop has many customers and a customer frequents many shops.

    The result of doing this the Rails way is you can retrieve customers belonging to a specific shop just by using an instance of that shop and calling shop.customers You can find a specific customer for that shop, by calling shop.customers.find(:id) If you try calling for another shops customer, you get a ActiveRecord::RecordNotFoundError

    Ditto the other way round. To retrieve the shops a customer frequents, ideally you want customer.shops etc as above. To add a new shop to a customer, use customer.shops.create("Arkwrights Dairy")

    As Shadwell stated your current efforts to create the structure are far more complicated than you need. And you do not set this up through the controllers, only through the models.

    Models, 2 only

    class Shop < ActiveRecord::Base
      has_and_belongs_to_many :customers
    
    class Customer < ActiveRecord::Base
      has_and_belongs_to_many :shops
    

    Migrations, 3

    class SampleMigration < ActiveRecord::Migration
      def change
        create_table :shops do |t|
          t.string :name
        end
    
        create_table :customers do |t|
          t.string  :name
        end
    
        create_table :customers_shops, id: false do |t|
          t.belongs_to :customer
          t.belongs_to :shop  
        end
      end
    end
    

    Add more detail to the models as you see fit, but this should be enough to establish the relationships. Things to watch are the plurals, shop verse shops is important. Shop the class and belongs_to :shop are both singular. References to many shops is plural and tables hold many shops so are also plural. And the join table has to be in alphabetical order, customers_shops not shops_customers.

    Once set up, run your migrations then try.

    shop = Shop.create(name: "Dairy")
    => #<Shop id:1, name: "Dairy">
    shop.customers.create(name: "Bill")
    => #<Customer id:1, name: "Bill">
    
    customer = Customer.find(1)
    => #<Customer id:1, name: "Bill">
    customer.shops
    => [#<Shop id:1, name: "Dairy">]
    

    Note that shops returns an array. Access the first element using customer.shops[0] or alternatively customer.shops.find(1) to retrieve the shop using its :id

    Hopefully that will give you the foundation you need :-)

    0 讨论(0)
  • 2021-01-15 02:12

    Your relationships method in your controller is all over the place.

    You're trying to find relationshipS using a find by id with a non-existent params[:relationship_id] which is causing the error you're seeing.

    You're then setting @users to be all the relationships for the @shop.

    Then you're rendering a template show_relationships but you refer later to a relationships/show template.

    Additionally in the shop model you're calling create on relationships with just a user id whereas you'd expect to be passing in some attributes for the relationship.

    It looks like this code has got messier and messier as you've tried to solve the problem. To be perfectly honest I'd go back to the beginning and start again.

    0 讨论(0)
提交回复
热议问题