Relationship in Rails, how to declare in Active Record?

0

I have a list of shops. Users are not registered in any of them at first. When he (the user) decides to register, a relationship is created between him and the establishment. It would be like Facebook's 'add friend' feature. But here's the question:

What would be the relationship of the tables in the database? What about Active Record to be more specific? Would it be a kind of dynamic relationship (if that exists)?

    
asked by anonymous 10.03.2014 / 00:18

1 answer

3

Let's use the following nomenclature:

  • Shop : Business establishment
  • CustomerLink : Business-to-customer association
  • Customer : Customer

No Active Record , your relationship would look like this:

class Shop < ActiveRecord::Base
  has_many :customerlinks
  has_many :customers, through: :customerlinks
end

class CustomerLink < ActiveRecord::Base
  belongs_to :shop
  belongs_to :customer
end

class Customer < ActiveRecord::Base
  has_many :customerlinks
  has_many :shops, through: :customerlinks
end
    
10.03.2014 / 01:03