过去几天来,我一直在看似简单的问题上困扰,请提供帮助。我有三种型号:
class Organization < ApplicationRecord
# resourcify
extend FriendlyId
friendly_id :slug_candidates, :use => [:slugged, :finders]
belongs_to :owner, class_name: 'User', foreign_key: 'owner_id', inverse_of: :organization
belongs_to :country
has_many :stores
has_many :organization_users
has_many :users, through: :organization_users
has_many :addresses, as: :addressable, dependent: :destroy
has_many :organization_catalogs
has_many :store_catalogs
has_one :account
has_one :subscription, through: :account
# accepts_nested_attributes_for :owner, :addresses
validates :name, presence: true
validates :name, uniqueness: true
validates :subdomain, uniqueness: true
after_create :create_master_catalog
after_create :create_account
def store_users
_users = {}
stores.each do |store|
_users["#{store.id}"] = User.store_users(store.id)
end
_users
end
def catalogs
Catalog.where organization_id: id
end
private
def slug_candidates
[
:subdomain,
:name
]
end
def create_master_catalog
OrganizationCatalog.create! name: 'Master Catalog', organization_id: self.id, master: true
end
def create_account
plan = Subscription::SUBSCRIPTION_PLANS[:trial]
trial_period = Subscription::TRIAL_PERIOD
subscription = Subscription.create! name: plan[:name], price: (plan[:price].to_i * 100), currency: 'INR', trial_start: Time.now, billing_cycle: 30.days.to_s, trial_end: ( Time.now + trial_period), trial_period: trial_period
Account.create! organization_id: self.id, subscription_id: subscription.id
end
end
# This class is for all clients
class User < ApplicationRecord
rolify
extend FriendlyId
friendly_id :uid, :use => [:slugged, :finders]
devise :invitable, :database_authenticatable, :recoverable, :rememberable, :validatable,
:lockable, :timeoutable, :trackable, :uid #, :omniauthable, :confirmable, :registerable,
attr_accessor :skip_password_validation, :role, :store_ids
has_many :invitations, class_name: 'User', as: :invited_by
has_one :organization_user, dependent: :destroy
has_one :organization, class_name: 'Organization', foreign_key: 'owner_id', inverse_of: :owner
has_one :organization, through: :organization_user
has_one :store_user, dependent: :destroy
has_one :store, through: :store_user
scope :organization_users, -> (organization_id){
includes(:organization_user)
.where(
organization_users: { organization_id: organization_id }
)
}
scope :store_users, -> (store_id){
includes(:store_user)
.where(
user_type: USER_TYPES[:store],
store_users: { store_id: store_id }
)
}
# before_create :verify_and_create_user_type
USER_TYPES = {
store: "Store",
organization: "Organization",
admin: "Admin"
}
validates :email, presence: true, uniqueness: true
validates :first_name, presence: true
validates :last_name, presence: true
# validates :user_type,
# :inclusion => { :in => USER_TYPES.values },
# :allow_nil => false,
# presence: true
before_invitation_created :assign_default_store_role
attr_reader :store_id
def assign_default_store_role
# self.add_role :employee
end
def store_user?
self.store_user.present?
end
def organization_user?
self.organization_user.present?
end
def display_name
"#{first_name} #{last_name}"
end
def admin?
admin
end
protected
def remove_previous_roles(role)
if role.resource
Rails.logger.debug "User::remove_previous_roles: Adding the role of #{role.name} for #{role.resource_type} #{role.resource_id} to user #{id}"
#remove any pre-existing role this user has to the resource
remove_role nil, role.resource
else
remove_role nil
end
end
def password_required?
return false if skip_password_validation
super
end
end
class OrganizationUser < ApplicationRecord
belongs_to :organization
belongs_to :user
end
在过去的两天里,我一直被这个错误困扰,而且我对解决该问题的想法不多。我不断收到ArgumentError wrong number of arguments (given 3, expected 2)
,但不知道为什么。错误甚至也没有任何回溯,所以我什至不知道它从哪里来。
错误似乎很简单,告诉我在只需要2个参数时我要传递3个参数。但是问题是当我收到此错误时,我什至没有传递任何参数。
在以下情况下会发生错误:
org = Organization.first
org.users
org = Organization.first
User.includes(:organization_user).where(organization_user: { organization_id: org.id})
user = User.first
user.add_role :manager
store = Store.first
store.users
这可能表明用户模型存在一些问题,但是我尝试了很多事情,但似乎都没有用。
日志文件错误:
上述错误的代码:
class OrganizationsController < ApplicationController
skip_before_action :authenticate_user!, only: [:new, :create, :details, :update]
load_and_authorize_resource except: [:new, :create, :details, :update]
layout :resolve_layout
def index
@stores = Store.all
end
def new
@org = Organization.new
@org.users.build
end
def create
@org = Organization.new(sign_up_params)
respond_to do |format|
if @org.save(validate: false)
add_owner_role @org
format.html { render action: :details }
else
format.html { render action: :new, alert: 'There were problems creating your account. Please rectify the errors.' }
format.json { render :json => @org.errors,
:status => :unprocessable_entity }
end
end
end
def show
# respond_to do |format|
# format.html # show.html.erb
# format.json { render :json => @store }
# end
end
def edit
end
def details
end
def update
@org = Organization.find params[:id]
respond_to do |format|
if @org.update(subdomain: organization_details_params[:subdomain])
@org.addresses << Address.create(organization_details_params[:address])
format.html { redirect_to(root_path,
:notice => 'Your account has been successfully created. Please continue by logging in.') }
format.json { head :no_content }
else
format.html { render :action => "details" }
format.json { render :json => @org.errors,
:status => :unprocessable_entity }
end
end
end
def destroy
# @store.destroy
# respond_to do |format|
# format.html { redirect_to stores_url }
# format.json { head :no_content }
# end
end
private
def add_owner_role organization
organization.owner.add_role :owner, organization
OrganizationUser.create!(organization_id: organization.id, user_id: organization.owner.id)
end
def store_params
params.require(:store).permit(:id, :name, :description)
end
def organization_details_params
params.require(:organization).permit(:subdomain, address: [:address, :address1, :address2, :city_id, :country_id, :state_id, :registered, :pincode])
end
def sign_up_params
params.require(:organization).permit(:name, :country_id, users_attributes: [:first_name, :last_name, :email, :mobile, :password, :password_confirmation])
end
def resolve_layout
case action_name
when "new", "create", "details"
"users"
else
"application"
end
end
end
但是User模型本身可以正常工作。就像我可以执行以下操作:
u = User.first
org = Organization.first
org.organization_users.first.user
#<User:0x00007fb7daa23570>
我不确定是否有帮助,但是我正在使用rails 6.0.0rc1和ruby 2.6.3。
请告知我是否还有其他调试方法。
答案 0 :(得分:0)
我仍然不确定问题的原因是什么,但是当我将Gemfile.lock恢复到先前状态后,问题就得到了解决。
我仍然从差异中看不到是什么原因造成的。只有我所拥有的更改才可以升级到几个宝石。
让我知道是否有人想看看他们。