Reputation: 3338
I have
class User::AuthInfo < ActiveRecord::Base
self.table_name = "auth_infos"
belongs_to :authenticable, polymorphic: true
end
and
class User::Customer < ActiveRecord::Base
self.table_name = "customers"
has_one :auth_info, as: :authenticable
end
I am expecting that by doing this:
User::Customer.last.auth_info
the output should be a record from auth_info
. But I see the query is wrong:
SELECT `auth_infos`.* FROM `auth_infos` WHERE `auth_infos`.`customer_id` = 8 LIMIT 1
My table has authenticable_id
and authenticable_type,
the fields that the docs tells me to create to make polymorphic work.
What is wrong? Why is it using customer_id
instead of authenticable_id
?
EDIT:
Here the schema of the two tables:
EDIT:
I did omit one thing, because I thinked that it was not important, but it seems to cause the problem, on the Customer model I am using a concern:
include User::Loggable
Here is the content of the concercn:
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
I've commended out the include for concern in my module, and it works. Why?
Upvotes: 2
Views: 83
Reputation: 841
Your concern is overriding association including in customer model.
included do
has_one :auth_info, as: :authenticable
end
Either provide polymorphic association inside concern or in real model.
Upvotes: 1