【问题标题】:How to make a relationship with Spree::User?如何与 Spree::User 建立关系?
【发布时间】:2020-02-05 21:36:43
【问题描述】:

我正在尝试在 Solidus 框架之上构建朋友功能,但在与 Spree::Users 建立多对多关系时遇到了麻烦。我尝试制作 user_decorator.rb 文件(在模型/spree 中),但一直遇到错误:“期望 user_decorator.rb 定义 Spree::UserDecorator,但没有”。

用户装饰器:

 Spree::User.class_eval do
   has_many :friendships
   has_many :friends, through: :friendships
 end

友谊模型:

 class Friendship < ActiveRecord::Base
   belongs_to :spree_user, :class_name => 'Spree::User'
   belongs_to :friend, :class_name => 'Spree::User'
 end

错误:

https://i.stack.imgur.com/QOpEU.png

【问题讨论】:

  • Rails 很简单,但如果你知道how_to :) 文件 user_decorator.rb 需要在其中定义 UserDecorator。如果您已经定义了它,那么它应该包含在其中。据我所知。

标签: ruby-on-rails solidus


【解决方案1】:

app/models/spree/user_decorator 中的内容替换为:

module Spree
  module UserDecorator
    def self.prepended(base)
      base.has_many :friendships
      base.has_many :friends, through: :friendships
    end

    Spree::User.prepend self
  end
end

【讨论】:

    猜你喜欢
    • 2021-12-20
    • 1970-01-01
    • 2014-02-22
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 2018-02-15
    • 2017-11-18
    • 2022-01-20
    相关资源
    最近更新 更多