this going kind of long, i'm self taught, , i'm afraid i'm making sort of fundamental error causing me lots of issues - i'm going thorough.
the setup (simplified of course - , had change names if see typo, that's not issue, sorry):
class deviceapi < activerecord::base belongs_to :devices end class device < activerecord::base has_and_belongs_to_many :users has_many :deviceapis end class user < activerecord::base has_many :templates, dependent: :destroy has_many :designs, through: :templates has_many :deviceapis, through: :devices has_and_belongs_to_many :devices end class template < activerecord::base belongs_to :user has_many :designs, dependent: :destroy end class design < activerecord::base belongs_to :template end the explanation:
my program receives data device - each data entry creates deviceapi entry in database. data used templates , designs. users don't have access devices (except admins), , no 1 accesses deviceapis @ all.
now, want able write queries such as:
class placement < activerecord::base . . # counts number of api entries associated design self.campaign.user.devices.deviceapis.count end further, admins should able restrict users access devices desired. should indirectly restrict access deviceapis, since accessible through devices anyway (they don't have view pages or anything).
specifically, schema (simplified) shows:
activerecord::schema.define(version: ################) create_table "deviceapis", force: :cascade |t| t.string "api_id" end create_table "devices", force: :cascade |t| t.string "device_guid" t.integer "user_ids" end create_table "designs", force: :cascade |t| t.string "device_id" end end queries should automatically restricted instances device_guid (device) , api_id (deviceapi) match. should further restricted user_ids (device) matching user_id (the primary key of user). more restricted device_id (design) matching device_id (primary key of device).
scopes , methods can handle queries once them working, can't queries come empty or spit out errors. think i'm needing join tables or something.
how build proper join tables this? or there other, more obvious way i'm not seeing? i've spent long time on this, , i'm learning lot in trial fire, not making progress on specific issue - , think it's crippling me in other places.
thanks help!
here think issue.
if log this: placement.campaign.user.devices results. ( don't know relation in placement , campaign guessing.
in console this: ( based on above assumptions ) placement.campaign.user.devices.class should : device::activerecord_associations_collectionproxy or similar.
you not able placement.campaign.user.devices.deviceapi rails don't know 1 of record need deviceapi.
if placement.campaign.user.devices.first.deviceapi should result if placement.campaign.user.devices.first.deviceapi.class should deviceapi or similar.
to query number of deviceapi use join condition.
something user.joins(devices: (:deviceapis) ).where('users.id = ?', current_user.id).count
i assumed have current_user available or put other condition think fits.
please let me know if helps.
Comments
Post a Comment