Rails 将 user_id 分配给来自获取的 IP 地址的调查事件

如何解决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,因为这是创建用户的方式,而不是从初始注册开始,尽管他们可能会在之后注册

不想强制注册参加调查。

代码位于 Github

欢迎指点。

谢谢

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?