简体   繁体   English

Rails API - 访问位于 model 中的方法?

[英]Rails API - accessing methods located in a model?

I've built a throwaway web app in rails to test out some new concepts.我在 Rails 中构建了一个一次性的 web 应用程序来测试一些新概念。 While doing this I created methods that belonged to the model, so as to keep to the principle of keeping controllers light and simple.在此过程中,我创建了属于 model 的方法,以保持控制器轻巧简单的原则。

However, now I am testing the same app but through a rails API.但是,现在我正在测试同一个应用程序,但通过 rails API。 Can I still keep these methods in the model?我还能在 model 中保留这些方法吗?

  1. I'm not sure how to route the API to access those methods.我不确定如何路由 API 来访问这些方法。
  2. Because it involves a nested model (contacts through users), I'm not sure where to start even putting it into a controller.因为它涉及嵌套的 model(通过用户联系),所以我不确定从哪里开始,甚至将它放入 controller。 Can I make a controller for a nested model?我可以为嵌套的 model 制作 controller 吗?

Here is the user model which gives a taste about what I'm talking about.这是用户 model ,它让您了解我在说什么。 Most of the methods are essential for the process of adding/accepting and creating contacts.大多数方法对于添加/接受和创建联系人的过程都是必不可少的。

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

Thanks!谢谢!

You need to create a call to the controller.您需要创建对 controller 的调用。 Lets consider the below example.让我们考虑下面的例子。

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

You need to define the routes to this controller in routes.rb您需要在routes.rb中定义到此 controller 的routes

You have created instance methods in the user model.您已在user model 中创建instance methods You can access them with the instance of user您可以使用user实例访问它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM