Ruby on rails Rails API-访问模型中的方法?

Ruby on rails Rails API-访问模型中的方法?,ruby-on-rails,Ruby On Rails,我已经在rails中构建了一个一次性web应用程序来测试一些新概念。在这样做的同时,我创建了属于模型的方法,以保持控制器轻巧和简单的原则 然而,现在我正在通过rails API测试同一个应用程序。我还能在模型中保留这些方法吗 我不知道如何路由API来访问这些方法 因为它涉及一个嵌套模型(通过用户联系),所以我甚至不确定从哪里开始将其放入控制器中。我可以为嵌套模型制作控制器吗 这是一个用户模型,它让我尝到了我所说的。在添加/接受和创建联系人的过程中,大多数方法都是必不可少的 class User

我已经在rails中构建了一个一次性web应用程序来测试一些新概念。在这样做的同时,我创建了属于模型的方法,以保持控制器轻巧和简单的原则

然而,现在我正在通过rails API测试同一个应用程序。我还能在模型中保留这些方法吗

  • 我不知道如何路由API来访问这些方法
  • 因为它涉及一个嵌套模型(通过用户联系),所以我甚至不确定从哪里开始将其放入控制器中。我可以为嵌套模型制作控制器吗
  • 这是一个用户模型,它让我尝到了我所说的。在添加/接受和创建联系人的过程中,大多数方法都是必不可少的

    class User < ApplicationRecord
    
      has_many :contactships, dependent: :destroy
      has_many :contacts, -> { where contactships: { status: :accepted }}, through: :contactships
      has_many :requested_contacts, -> { where contactships: { status: :requested }}, through: :contactships, source: :contact
      has_many :pending_contacts, -> { where contactships: { status: :pending }}, through: :contactships, source: :contact
      has_many :blocked_contacts, -> { where contactships: { status: :blocked }}, through: :contactships, source: :contact
    
      has_many :contactships_inverse, class_name: 'Contactship', foreign_key: :contact_id
      has_many :contacts_inverse, through: :contactships_inverse, source: :user
    
      has_one_attached :avatar
    
      validates_presence_of :first_name, :last_name
    
      def full_name
        "#{first_name} #{last_name}"
      end
    
      def all_contacts
        contacts + contacts_inverse
      end
    
      def has_contactship?(contact)
          #return true if the user is a contact
          return true if self == contact
          contactships.map(&:contact_id).include?(contact.id)
      end
    
      def requested_contacts_with?(contact)
          return false if self == contact
          #we are going to map requested contacts with list of users to see if they include contact_id
          requested_contacts.map(&:id).include?(contact.id)
      end
    
      def pending_contacts_with?(contact)
          return false if self == contact
          pending_contacts.map(&:id).include?(contact.id)
      end
    
      def contacts_with?(contact)
          return false if self == contact
          contacts.map(&:id).include?(contact.id)
      end
    
      def contact_request(contact)
        #unless the contact is not equal to self and contactship does not already exist
        unless self == contact || Contactship.where(user: self, contact: contact).exists?
            #transaction means that if one fails they both are rolled back
            transaction do
                #for user to another user (sent request)
                Contactship.create(user: self, contact: contact, status: :pending)
                #from another user to user (recieve request)
                Contactship.create(user: contact, contact: self, status: :requested)
            end
         end
      end
    
      def accept_request(contact)
          transaction do
            Contactship.find_by(user: self, contact: contact, status: [:requested])&.accepted!
            Contactship.find_by(user: contact, contact: self, status: [:pending])&.accepted!
          end
      end
    
      def reject_request(contact)
          transaction do
            Contactship.find_by(user: self, contact: contact)&.destroy!
            Contactship.find_by(user: contact, contact: self)&.destroy!
          end
      end
    
    end
    
    class用户{where contactships:{status::accepted}},通过::contactships
    有很多:请求的联系人,->{where contactships:{status::requested}},通过::contactships,source::contact
    有很多:待定联系人,->{where contactships:{status::pending}}},通过::contactships,source::contact
    有很多:阻止的联系人,->{where contactships:{status::blocked}},通过::contactships,source::contact
    有很多:联系人姓名,类别名称:'Contactship',外键::联系人id
    有很多:联系人,通过::联系人,来源::用户
    有没有附加一个:阿凡达
    验证是否存在:first\u name、:last\u name
    def全名
    “#{姓}#{姓}”
    结束
    def所有_触点
    触点+触点\u反向
    结束
    def有_联系人?(联系人)
    #如果用户是联系人,则返回true
    如果self==contact,则返回true
    contactships.map(&:contact_id)。包括?(contact.id)
    结束
    请求的def联系人?(联系人)
    如果self==contact,则返回false
    #我们将把请求的联系人映射到用户列表中,看看他们是否包含联系人id
    请求的_contacts.map(&:id)。包括?(contact.id)
    结束
    def待定联系人?(联系人)
    如果self==contact,则返回false
    待定的联系人映射(&:id).include?(contact.id)
    结束
    def联系人与?(联系人)
    如果self==contact,则返回false
    contacts.map(&:id).include?(contact.id)
    结束
    def联系人请求(联系人)
    #除非联系人不等于self,并且联系人不存在
    除非self==contact | | Contactship.where(用户:self,联系人:contact.)存在?
    #事务意味着,如果其中一个失败,它们都将回滚
    事务处理
    #对于用户到另一个用户(已发送请求)
    Contactship.create(用户:self,联系人:contact,状态::pending)
    #从另一个用户到另一个用户(接收请求)
    Contactship.create(用户:contact,联系人:self,状态::requested)
    结束
    结束
    结束
    def接受_请求(联系方式)
    事务处理
    Contactship.find_by(用户:self,联系人:contact,状态:[:请求])和接受!
    Contactship.find_by(用户:contact,联系人:self,状态:[:待定])和接受!
    结束
    结束
    def拒绝_请求(联系方式)
    事务处理
    Contactship.find_by(用户:self,联系人:contact)和销毁!
    Contactship.find_by(用户:contact,contact:self)和销毁!
    结束
    结束
    结束
    

    谢谢

    您需要创建对控制器的调用。让我们考虑下面的例子。

    class UsersController < ApplicationController
    
      def method_name
        @user = User.find(params[:id])
        # Now you can access all the user model methods using @user.model_method_name
        @user.all_contacts
      end
    
    end
    
    class UsersController
    您需要在
    routes.rb


    您已经在
    用户
    模型中创建了
    实例方法
    。您可以通过
    user

    的实例访问它们,您需要创建对控制器的调用。让我们考虑下面的例子。

    class UsersController < ApplicationController
    
      def method_name
        @user = User.find(params[:id])
        # Now you can access all the user model methods using @user.model_method_name
        @user.all_contacts
      end
    
    end
    
    class UsersController
    您需要在
    routes.rb


    您已经在
    用户
    模型中创建了
    实例方法
    。您可以通过
    user

    的实例访问它们。当然,如果控制器本身不是一个模型,它会被称为什么?contactships\u controller?因为您已在用户模型中添加了方法。您应该从用户控制器调用它们,但这不是必需的。您可以在任何控制器中创建用户实例,并可以访问该控制器中的所有方法,无需移动模型方法。,只需在任何控制器中创建用户实例,然后您就可以访问所有模型实例方法。在整个代码库中,您在模型文件中定义的任何公共方法都将对该模型的任何实例可用。@JamesStirrat不要挂断控制器的名称,您可以调用描述其工作的任何名称。如果您很难将其命名为UsersController,那么请给它一个您喜欢的名称,只要config/routes.rb正确指向控制器。有意义的描述比(弱)rails命名约定更重要。有时我有多个控制器控制一个模型,对应不同的工作流,这样做比重载一个控制器更好。当然,如果控制器本身不是模型,它会被称为什么?contactships\u controller?因为您已在用户模型中添加了方法。您应该从用户控制器调用它们,但这不是必需的。您可以在任何控制器中创建用户实例,并可以访问该控制器中的所有方法无需移动模型方法,只需在任何控制器中创建用户实例即可