Find a model by its referenced model (child?) in Ruby on Rails -
rails nooby here, looking tips. i've looked around on site , rails website , can't seem find i'm looking for.
i have account class
class account < activerecord::base has_one :access_token, dependent: :destroy has_secure_password
and of course
class accesstoken < activerecord::base
what want able in controllers account.find_by_access_token
or that. there way in rails?
assuming column's name accesstoken
named token
, try:
class account < activerecord::base def self.find_by_token(token) account.joins(:access_token).where(access_tokens: { token: token } ) end end
and use this:
account.find_by_token('xxxxxxx')
Comments
Post a Comment