ruby - 这个多态关联有什么问题?

标签 ruby ruby-on-rails-4 associations polymorphic-associations

我有

class User::AuthInfo < ActiveRecord::Base
    self.table_name = "auth_infos"
    belongs_to :authenticable, polymorphic: true
end

class User::Customer < ActiveRecord::Base
    self.table_name = "customers"

    has_one :auth_info, as: :authenticable
end

我希望这样做:

User::Customer.last.auth_info

输出应该是来自 auth_info 的记录。但我看到查询是错误的:

SELECT  `auth_infos`.* FROM `auth_infos` WHERE `auth_infos`.`customer_id` = 8 LIMIT 1

我的表有 authenticable_idauthenticable_type, 文档告诉我创建这些字段以进行多态性工作。

怎么了?为什么使用 customer_id 而不是 authenticable_id

编辑:

这里是两个表的模式:

auth_infos

customers

编辑:

我确实省略了一件事,因为我认为它不重要,但它似乎导致了问题,在客户模型上我使用了一个问题:

include User::Loggable

关注内容如下:

module User::Loggable
    extend ActiveSupport::Concern

    included do 
        has_one :auth_info 
    end

    def login ip_address
        @session_ip = ip_address
        create_session_token
        self.save
    end

    def renew_session
        @session_token_expire = renew_session
    end

    def destroy_session
        self.session_token = nil
        self.session_token_expire = nil
        self.save
    end

    def verify_session! (token, ip)
        if (ip == session_ip && token = session_token && session_token_expire.to_i > Time.now.to_i)
            true
        else
            false
        end 
    end


    private 

    def new_token( string = "" )
      ...
    end

    def digest_token token
        ..
    end  

    def register_user
        a = User.new email_address: self.email_address, kind: self.class.name
        a.save
    end

    def create_session_token
        @session_token = digest_token(new_token())
        @session_token_expire = (Time.now + AUTHENTICATION[::Rails.env]["expiration"]["session"].hour.to_i).strftime("%Y-%m-%d %H:%M:%S")
    end

    def renew_session
        session_token_expire = (Time.now + AUTHENTICATION[::Rails.env]["expiration"]["session"] + 3.hour).strftime("%Y-%m-%d %H:%M:%S")
    end

    module ClassMethods     
        def authenticated_account
            current_account ||= self.find_by_session_token() if CbsoltCore::App::Environment.account_session_token && CbsoltCore::App::Environment.account_session_token != ""

            # mettere parte API token
            current_account
        end
    end 
end

我已经在我的模块中推荐了 include for concern,并且它有效。为什么?

最佳答案

您关心的是覆盖客户模型中的关联。

included do 
    has_one :auth_info, as: :authenticable
end

在关注点或真实模型中提供多态关联。

关于ruby - 这个多态关联有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30728270/

相关文章:

ruby-on-rails - 你如何在 rails 3.1 中删除 `references` 列?

ruby - 为什么在此代码中需要 klass?

javascript - 如何在 haml 的内联 JavaScript 中使用 Ruby 变量?

ruby - 无法在 Windows 上使用 Ctrl+C 中断/终止 Ruby 脚本

ruby-on-rails - 设计 Flash 方法 Rails

jquery - 使用Raty jquery插件和rails 4.0脚手架形式

ruby-on-rails - rails : How to limit number of items in has_many association (from Parent)

java - 玩!删除时的框架参照完整性约束

Redis 和关系

ruby - 在给定超时后逃离 Ruby 的 TCPSocket::gets 的最佳方法是什么?