Ruby on rails 如何在概念上为客户-商户交易模型建模,例如在ActiveRecord中?

Ruby on rails 如何在概念上为客户-商户交易模型建模,例如在ActiveRecord中?,ruby-on-rails,rails-activerecord,activejdbc,Ruby On Rails,Rails Activerecord,Activejdbc,我有一个客户模型、一个商户模型和一个交易模型。客户可以向商家进行交易,商家也可以向客户进行交易。最后,我想提出如下问题: 客户进行的所有交易 商户进行的所有交易 客户X与商户Y的所有交易(反之亦然) 可选:首先使用特定标记结束所有事务, 然后查找所有与 这些交易。这是一个很好的特性,但是如果它太 答案很难解释,那就不用担心了 那么从概念上讲,我应该如何为这些模型中的每一个创建关联呢?像客户一样有很多:商家,通过::交易(反之亦然)?还是使用多态关联?等等 非常感谢你的帮助 从概念上讲,交易模型连

我有一个客户模型、一个商户模型和一个交易模型。客户可以向商家进行交易,商家也可以向客户进行交易。最后,我想提出如下问题:

  • 客户进行的所有交易
  • 商户进行的所有交易
  • 客户X与商户Y的所有交易(反之亦然)
  • 可选:首先使用特定标记结束所有事务, 然后查找所有与 这些交易。这是一个很好的特性,但是如果它太 答案很难解释,那就不用担心了
  • 那么从概念上讲,我应该如何为这些模型中的每一个创建关联呢?像客户一样有很多:商家,通过::交易(反之亦然)?还是使用多态关联?等等


    非常感谢你的帮助

    从概念上讲,交易模型连接了客户和商户,因此:

    class Customer < ActiveRecord::Base
      has_many :transations
      has_many :merchants, through: :transactions
    end
    
    class Merchant < ActiveRecord::Base
      has_many :transactions
      has_many :customers, through: :transactions
    end
    
    class Transaction < ActiveRecord::Base
      belongs_to :customer
      belongs_to :merchant
    end
    
    要查找商户的所有交易,请执行以下操作:

    customer.transactions
    
    merchant.transactions
    
    customer.getAll(Transaction.class);
    
    merchant.getAll(Transaction.class);
    
    为商户25查找客户1的所有交易记录

    为客户8查找商户1的所有交易记录

    按标记查找所有事务(假设标记是字符串字段):

    查找所有交易标记为“畅销书”的商户:

    merchants = Merchant.includes(:transaction).where(transaction: {tag: 'best-seller'})
    
    customers = Customer.includes(:transation).where(transaction: {tag: 'best-seller'})
    
    transactions = Transaction.where("tag = 'best-seller'").include(Merchant.class);
    //iterate over transactions, and get a merchants:
    transactions.get(i).getAll(Merchant.class)
    
    查找标有“畅销书”的交易的所有客户:

    merchants = Merchant.includes(:transaction).where(transaction: {tag: 'best-seller'})
    
    customers = Customer.includes(:transation).where(transaction: {tag: 'best-seller'})
    
    transactions = Transaction.where("tag = 'best-seller'").include(Merchant.class);
    //iterate over transactions, and get a merchants:
    transactions.get(i).getAll(Merchant.class)
    

    从概念上讲,交易模型连接了客户和商户,因此:

    class Customer < ActiveRecord::Base
      has_many :transations
      has_many :merchants, through: :transactions
    end
    
    class Merchant < ActiveRecord::Base
      has_many :transactions
      has_many :customers, through: :transactions
    end
    
    class Transaction < ActiveRecord::Base
      belongs_to :customer
      belongs_to :merchant
    end
    
    要查找商户的所有交易,请执行以下操作:

    customer.transactions
    
    merchant.transactions
    
    customer.getAll(Transaction.class);
    
    merchant.getAll(Transaction.class);
    
    为商户25查找客户1的所有交易记录

    为客户8查找商户1的所有交易记录

    按标记查找所有事务(假设标记是字符串字段):

    查找所有交易标记为“畅销书”的商户:

    merchants = Merchant.includes(:transaction).where(transaction: {tag: 'best-seller'})
    
    customers = Customer.includes(:transation).where(transaction: {tag: 'best-seller'})
    
    transactions = Transaction.where("tag = 'best-seller'").include(Merchant.class);
    //iterate over transactions, and get a merchants:
    transactions.get(i).getAll(Merchant.class)
    
    查找标有“畅销书”的交易的所有客户:

    merchants = Merchant.includes(:transaction).where(transaction: {tag: 'best-seller'})
    
    customers = Customer.includes(:transation).where(transaction: {tag: 'best-seller'})
    
    transactions = Transaction.where("tag = 'best-seller'").include(Merchant.class);
    //iterate over transactions, and get a merchants:
    transactions.get(i).getAll(Merchant.class)
    

    从概念上讲,交易模型连接了客户和商户,因此:

    class Customer < ActiveRecord::Base
      has_many :transations
      has_many :merchants, through: :transactions
    end
    
    class Merchant < ActiveRecord::Base
      has_many :transactions
      has_many :customers, through: :transactions
    end
    
    class Transaction < ActiveRecord::Base
      belongs_to :customer
      belongs_to :merchant
    end
    
    要查找商户的所有交易,请执行以下操作:

    customer.transactions
    
    merchant.transactions
    
    customer.getAll(Transaction.class);
    
    merchant.getAll(Transaction.class);
    
    为商户25查找客户1的所有交易记录

    为客户8查找商户1的所有交易记录

    按标记查找所有事务(假设标记是字符串字段):

    查找所有交易标记为“畅销书”的商户:

    merchants = Merchant.includes(:transaction).where(transaction: {tag: 'best-seller'})
    
    customers = Customer.includes(:transation).where(transaction: {tag: 'best-seller'})
    
    transactions = Transaction.where("tag = 'best-seller'").include(Merchant.class);
    //iterate over transactions, and get a merchants:
    transactions.get(i).getAll(Merchant.class)
    
    查找标有“畅销书”的交易的所有客户:

    merchants = Merchant.includes(:transaction).where(transaction: {tag: 'best-seller'})
    
    customers = Customer.includes(:transation).where(transaction: {tag: 'best-seller'})
    
    transactions = Transaction.where("tag = 'best-seller'").include(Merchant.class);
    //iterate over transactions, and get a merchants:
    transactions.get(i).getAll(Merchant.class)
    

    从概念上讲,交易模型连接了客户和商户,因此:

    class Customer < ActiveRecord::Base
      has_many :transations
      has_many :merchants, through: :transactions
    end
    
    class Merchant < ActiveRecord::Base
      has_many :transactions
      has_many :customers, through: :transactions
    end
    
    class Transaction < ActiveRecord::Base
      belongs_to :customer
      belongs_to :merchant
    end
    
    要查找商户的所有交易,请执行以下操作:

    customer.transactions
    
    merchant.transactions
    
    customer.getAll(Transaction.class);
    
    merchant.getAll(Transaction.class);
    
    为商户25查找客户1的所有交易记录

    为客户8查找商户1的所有交易记录

    按标记查找所有事务(假设标记是字符串字段):

    查找所有交易标记为“畅销书”的商户:

    merchants = Merchant.includes(:transaction).where(transaction: {tag: 'best-seller'})
    
    customers = Customer.includes(:transation).where(transaction: {tag: 'best-seller'})
    
    transactions = Transaction.where("tag = 'best-seller'").include(Merchant.class);
    //iterate over transactions, and get a merchants:
    transactions.get(i).getAll(Merchant.class)
    
    查找标有“畅销书”的交易的所有客户:

    merchants = Merchant.includes(:transaction).where(transaction: {tag: 'best-seller'})
    
    customers = Customer.includes(:transation).where(transaction: {tag: 'best-seller'})
    
    transactions = Transaction.where("tag = 'best-seller'").include(Merchant.class);
    //iterate over transactions, and get a merchants:
    transactions.get(i).getAll(Merchant.class)
    

    好的,@infused的建议看起来很棒。这里也一样

    首先定义模型:

    public class Customer extends Model {}
    public class Merchant extends Model {}
    public class Transaction extends Model {}
    
    其次,创建表:

    顾客:

    id | first_name | last_name | etc
    
    商户:

    id | name | address1 | etc
    
    交易:

    id | customer_id | merchant_id | tag | etc
    
    查找客户的所有交易记录:

    customer.transactions
    
    merchant.transactions
    
    customer.getAll(Transaction.class);
    
    merchant.getAll(Transaction.class);
    
    要查找商户的所有交易,请执行以下操作:

    customer.transactions
    
    merchant.transactions
    
    customer.getAll(Transaction.class);
    
    merchant.getAll(Transaction.class);
    
    为商户25查找客户1的所有交易记录

    为客户8查找商户1的所有交易记录

    按标记查找所有事务(假设标记是字符串字段):

    查找所有交易标记为“畅销书”的商户:

    merchants = Merchant.includes(:transaction).where(transaction: {tag: 'best-seller'})
    
    customers = Customer.includes(:transation).where(transaction: {tag: 'best-seller'})
    
    transactions = Transaction.where("tag = 'best-seller'").include(Merchant.class);
    //iterate over transactions, and get a merchants:
    transactions.get(i).getAll(Merchant.class)
    
    这种方法将只运行2个SQL查询,而且速度非常快


    我希望这有帮助

    好的,@infused的建议看起来很棒。这里也一样

    首先定义模型:

    public class Customer extends Model {}
    public class Merchant extends Model {}
    public class Transaction extends Model {}
    
    其次,创建表:

    顾客:

    id | first_name | last_name | etc
    
    商户:

    id | name | address1 | etc
    
    交易:

    id | customer_id | merchant_id | tag | etc
    
    查找客户的所有交易记录:

    customer.transactions
    
    merchant.transactions
    
    customer.getAll(Transaction.class);
    
    merchant.getAll(Transaction.class);
    
    要查找商户的所有交易,请执行以下操作:

    customer.transactions
    
    merchant.transactions
    
    customer.getAll(Transaction.class);
    
    merchant.getAll(Transaction.class);
    
    为商户25查找客户1的所有交易记录

    为客户8查找商户1的所有交易记录

    按标记查找所有事务(假设标记是字符串字段):

    查找所有交易标记为“畅销书”的商户:

    merchants = Merchant.includes(:transaction).where(transaction: {tag: 'best-seller'})
    
    customers = Customer.includes(:transation).where(transaction: {tag: 'best-seller'})
    
    transactions = Transaction.where("tag = 'best-seller'").include(Merchant.class);
    //iterate over transactions, and get a merchants:
    transactions.get(i).getAll(Merchant.class)
    
    这种方法将只运行2个SQL查询,而且速度非常快


    我希望这有帮助

    好的,@infused的建议看起来很棒。这里也一样

    首先定义模型:

    public class Customer extends Model {}
    public class Merchant extends Model {}
    public class Transaction extends Model {}
    
    其次,创建表:

    顾客:

    id | first_name | last_name | etc
    
    商户:

    id | name | address1 | etc
    
    交易:

    id | customer_id | merchant_id | tag | etc
    
    查找客户的所有交易记录:

    customer.transactions
    
    merchant.transactions
    
    customer.getAll(Transaction.class);
    
    merchant.getAll(Transaction.class);
    
    要查找商户的所有交易,请执行以下操作:

    customer.transactions
    
    merchant.transactions
    
    customer.getAll(Transaction.class);
    
    merchant.getAll(Transaction.class);
    
    为商户25查找客户1的所有交易记录

    为客户8查找商户1的所有交易记录

    按标记查找所有事务(假设标记是字符串字段):

    查找所有交易标记为“畅销书”的商户:

    merchants = Merchant.includes(:transaction).where(transaction: {tag: 'best-seller'})
    
    customers = Customer.includes(:transation).where(transaction: {tag: 'best-seller'})
    
    transactions = Transaction.where("tag = 'best-seller'").include(Merchant.class);
    //iterate over transactions, and get a merchants:
    transactions.get(i).getAll(Merchant.class)
    
    这种方法将只运行2个SQL查询,而且速度非常快


    我希望这有帮助

    好的,@infused的建议看起来很棒。这里也一样

    首先定义模型:

    public class Customer extends Model {}
    public class Merchant extends Model {}
    public class Transaction extends Model {}
    
    其次,创建表:

    顾客:

    id | first_name | last_name | etc
    
    商户:

    id | name | address1 | etc
    
    交易:

    id | customer_id | merchant_id | tag | etc
    
    查找客户的所有交易记录:

    customer.transactions
    
    merchant.transactions
    
    customer.getAll(Transaction.class);
    
    merchant.getAll(Transaction.class);
    
    要查找商户的所有交易,请执行以下操作:

    customer.transactions
    
    merchant.transactions
    
    customer.getAll(Transaction.class);
    
    merchant.getAll(Transaction.class);
    
    为商户25查找客户1的所有交易记录

    为客户8查找商户1的所有交易记录

    按标记查找所有事务(假设标记是字符串字段):

    查找所有交易标记为“畅销书”的商户:

    merchants = Merchant.includes(:transaction).where(transaction: {tag: 'best-seller'})
    
    customers = Customer.includes(:transation).where(transaction: {tag: 'best-seller'})
    
    transactions = Transaction.where("tag = 'best-seller'").include(Merchant.class);
    //iterate over transactions, and get a merchants:
    transactions.get(i).getAll(Merchant.class)
    
    这种方法将只运行2个SQL查询,而且速度非常快


    我希望这对我使用ActiveJdbc作为ORM的方式有所帮助。它与activerecord非常相似,因此任何使用activerecord的解决方案都将非常棒。但是,如果您知道如何使用activejdbc来实现这一点,那就更好了。谢谢:)顺便说一下,我使用ActiveJdbc作为我的ORM。它与activerecord非常相似,因此任何使用activerecord的解决方案都将非常棒。但是,如果您知道如何使用activejdbc来实现这一点,那就更好了。谢谢:)顺便说一下,我使用ActiveJdbc作为我的ORM。它与activerecord非常相似,因此任何使用activerecord的解决方案都将非常棒。但是,如果您知道如何使用activejdbc来实现这一点,那就更好了。