如何解决Rails 将 user_id 分配给来自获取的 IP 地址的调查事件
我正在 Rails 中构建一个调查事件模型,并对其进行设置,以便在(未注册的)用户提交数据时,他们的 IP 地址也会在调查事件期间附加到调查事件 json 负载。
发布请求是 localhost:3000/surveys/create
我还想做的是从他们的 IP 地址中创建一个 user_id 来添加,这样他们的调查选择是唯一的,如果他们以后用他们的电子邮件/姓名注册就可以使用。下面是基本事件的代码,然后是调查基本事件和创建的模型为其提交设置有效负载。
Models/events/base_event.rb
class Events::BaseEvent < ActiveRecord::Base
before_validation :find_or_build_aggregate
before_create :apply_and_persist
self.abstract_class = true
def apply(aggregate)
raise NotImplementedError
end
after_initialize do
self.event_type = event_type
self.payload ||= {}
end
def self.payload_attributes(*attributes)
@payload_attributes ||= []
attributes.map(&:to_s).each do |attribute|
@payload_attributes << attribute unless @payload_attributes.include?(attribute)
define_method attribute do
self.payload ||= {}
self.payload[attribute]
end
define_method "#{attribute}=" do |argument|
self.payload ||= {}
self.payload[attribute] = argument
end
end
@payload_attributes
end
private def find_or_build_aggregate
self.aggregate = find_aggregate if aggregate_id.present?
self.aggregate = build_aggregate if self.aggregate.nil?
end
def find_aggregate
klass = aggregate_name.to_s.classify.constantize
klass.find(aggregate_id)
end
def build_aggregate
public_send "build_#{aggregate_name}"
end
private def apply_and_persist
# Lock the database row! (OK because we're in an ActiveRecord callback chain transaction)
aggregate.lock! if aggregate.persisted?
# Apply!
self.aggregate = apply(aggregate)
#Persist!
aggregate.save!
self.aggregate_id = aggregate.id if aggregate_id.nil?
end
def aggregate=(model)
public_send "#{aggregate_name}=",model
end
def aggregate
public_send aggregate_name
end
def aggregate_id=(id)
public_send "#{aggregate_name}_id=",id
end
def aggregate_id
public_send "#{aggregate_name}_id"
end
def self.aggregate_name
inferred_aggregate = reflect_on_all_associations(:belongs_to).first
raise "Events must belong to an aggregate" if inferred_aggregate.nil?
inferred_aggregate.name
end
delegate :aggregate_name,to: :class
def event_type
self.attributes["event_type"] || self.class.to_s.split("::").last
end
def event_klass
klass = self.class.to_s.split("::")
klass[-1] = event_type
klass.join('::').constantize
end
end
Then inside survey folder /models/events/survey is a base_event which sets up the payload
class Events::Survey::BaseEvent < Events::BaseEvent
self.table_name = "survey_events"
belongs_to :survey,class_name: "::Survey"
#has_many :users
end
and the created model which submits to db
class Events::Survey::Created < Events::Survey::BaseEvent
payload_attributes :fuel_type,:boiler_type,:current_room,:resettle,:convert_to_combi,:water_veLocity,:new_room,:property_type,:over_sec_floor,:flue_ext_access,:flue_location,:flue_roof,:flue_position,:num_bed,:num_bath,:num_shower,:therm_valve,:user_ip
def apply(survey)
survey.fuel_type = fuel_type #string
survey.boiler_type = boiler_type #string
survey.current_room = current_room #string
survey.resettle = resettle #boolean
survey.convert_to_combi = convert_to_combi #boolean
survey.water_veLocity = water_veLocity #string
survey.new_room = new_room #string
survey.property_type = property_type #string
survey.over_sec_floor = over_sec_floor #boolean
survey.flue_ext_access = flue_ext_access #boolean
survey.flue_location = flue_location #string
survey.flue_roof = flue_roof #string
survey.flue_position = flue_position #string
survey.num_bed = num_bed #integer
survey.num_bath = num_bath #integer
survey.num_shower = num_shower #integer
survey.therm_valve = therm_valve
survey.user_ip = user_ip #string
survey
end
end
User model is just "belongs_to :survey" nothing else and controllers are below
Surveys controller
class SurveysController < ApplicationController
skip_before_action :verify_authenticity_token,only: [:create,:destroy]
def create
user_ip = request.remote_ip
Events::Survey::Created.create(payload: survey_params)
end
def destroy
end
private def survey_params
params.require(:survey).permit( :fuel_type,:user_ip )
end
end
Users controller
class UsersController < ApplicationController
skip_before_action :verify_authenticity_token,:destroy]
def user_params
params.require(:user).permit(:name,:email )
end
end
我想要做的是以某种方式从 IP 地址获取并分配一个用户 ID,因为这是创建用户的方式,而不是从初始注册开始,尽管他们可能会在之后注册。
不想强制注册参加调查。
欢迎指点。
谢谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。