调用模型方法并传递参数



我使用Graphql突变来保存User,看起来有点像这样:

class CreateUser < Mutations::BaseMutation
argument :email, String, required: true
argument :password, String, required: true
argument :password_confirmation, String, required: true
argument :first_name, String, required: false
argument :last_name, String, required: false
argument :middle_name, String, required: false
argument :source, String, required: false
field :user, Types::UserType, null: true
field :token, String, null: true
def resolve(args)
user = User.new(password: args[:password], password_confirmation: args[:password_confirmation], email: args[:email])

profile = user.build_profile
profile.first_name = args[:first_name] if args[:first_name].present?
profile.last_name = args[:last_name] if args[:last_name].present?
profile.middle_name = args[:middle_name] if args[:middle_name].present?
user.save!

UserMailer.with(user: user).send_initial_password_instructions.deliver_now if args[:source].present?

# current_user needs to be set so authenticationToken can be returned
context[:current_user] = user

MutationResult.call(obj: { user: user, token: user.authentication_token }, success: user.persisted?, errors: user.errors.full_messages)
end
end

这里一切都好。但是…我有一个模型叫Contact

class Contact < ApplicationRecord
belongs to :user, optional: true
end

所以我要做的是在Contact上创建一个方法,每当我创建User时,我可以将一些args发送给Contact并让Contact方法执行save!

这就是我一直在尝试的:Contact.rb

def self.create_with_user(args)
contact = Contact.new(args)
contact.user = User.new(email: args[:email], password: args[:password], password_confirmation: args[:password_confirmation])
contact.user.save!
end

这是我一直在尝试:create_user。Rb(图ql突变)

def resolve(args)
user = User.new(password: args[:password], password_confirmation: args[:password_confirmation], email: args[:email])
profile = user.build_profile
profile.first_name = args[:first_name] if args[:first_name].present?
profile.last_name = args[:last_name] if args[:last_name].present?
profile.middle_name = args[:middle_name] if args[:middle_name].present?
contact = Contact.create_with_user(args)
user.contact = contact
user.save!
UserMailer.with(user: user).send_initial_password_instructions.deliver_now if args[:source].present?

# current_user needs to be set so authenticationToken can be returned
context[:current_user] = user
MutationResult.call(obj: { user: user, token: user.authentication_token }, success: user.persisted?, errors: user.errors.full_messages)
end

但是这导致了一个NoMethodError Exception: undefined method 'to' for Contact:Class。新手rails在这里,所以我很有兴趣学习这个。谢谢

这个错误是由contact.rb生成的,你有一个类型是belongs_to,但你有belongs to

contact.rb

class Contact < ApplicationRecord
belongs to :user, optional: true
end
<<p>

首选解决方案/strong>如果Contact的创建应该总是发生,并且您没有来自端点的任何自定义参数,则在User模型上使用回调。

user.rb

class User < ApplicationRecord
after_create :create_contact
private
def create_contact
Contact.create(user: self, .....) # the other params you need to pass from model
end
end

如果你有自定义参数从端点,你可以使用accepts_nested_attributes_for,并使联系人参数嵌套在用户。

params = {
user: {
user_params,
contact: {
contact_params
}
}
}

user.rb

class User < ApplicationRecord
accepts_nested_attributes_for :contact
end

最新更新