2016-07-06 96 views
0

我有一個共享引擎,我保持共享AR模型和一堆其他東西。 下面是我的用戶AR模型從Rails引擎模型繼承時的關聯問題

module Shared 
    class User < ApplicationRecord 
    validates_presence_of :password, on: :create 
    # validates_uniqueness_of :email 
    # validates_uniqueness_of :phone_number 

    has_many :apps 
    end 
end 

而且我在主樑的應用程序(Analytics)兒童用戶,如下所示:

class User < Shared::User 
    self.table_name = 'users' 
end 

當我安裝這臺發動機的Rails應用程序,我動態用下面的方法找到的「正確」(Analytics::User):

def model_const(const) 
    # shell_app is set to 'Analytics' somewhere else 
    shell_app.constantize.const_get const 
rescue 
    Shared.const_get const 
end 

通過上面,我可以插入,找到數據庫中的記錄,但是當我使用assoc命令的問題開始和儀器選型的AR驗證(以上validates_uniqueness_of呼籲實例)

user = model_const('User').create ... # works, returns a Analytics::User instance 
user.apps # fails with below error 

(同樣的錯誤,如果在共享::用戶模式,我未評論驗證,並嘗試建立一個新的記錄)

ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: relation "shared_apps" does not exist 
LINE 8:    WHERE a.attrelid = '"shared_apps"'::regclas... 
            ^
:    SELECT a.attname, format_type(a.atttypid, a.atttypmod), 
       pg_get_expr(d.adbin, d.adrelid), a.attnotnull,  a.atttypid, a.atttypmod, 
     (SELECT c.collname FROM pg_collation c, pg_type t 
      WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation), 
       col_description(a.attrelid, a.attnum) AS comment 
      FROM pg_attribute a LEFT JOIN pg_attrdef d 
       ON a.attrelid = d.adrelid AND a.attnum = d.adnum 
      WHERE a.attrelid = '"shared_apps"'::regclass 
      AND a.attnum > 0 AND NOT a.attisdropped 
      ORDER BY a.attnum 
): 

而且請注意,我正在創建並嘗試在共享引擎代碼中訪問apps,不確定是否重要。

在此先感謝!

回答

0

花了好幾個小時後,我想出了這個解決方案。 (其實,這是我應該首先做的是,讓blame me

  1. 巢外User類分析模塊中:

    module Analytics class User.... end end

  2. 它添加到lib/shared.rb file

    def self.table_name_prefix end

希望這可能對別人有幫助..