All,
I'm still working my way around learning Rails and I'm not having much luck finding the relevant answers I need; that said, I suspect it's something that's been done before since I've done this by hand many times in the past myself.
I have a table called tab_accounts with account_id(int) as PK. I also have a lookup table called mtom_account_relations. This table has two int columns (account_subject, account_associate), both of which are FK to tab_accounts.account_id. The purpose of this layout is to allow for a many-to-many relationship between tab_account entries. The goal is to create an endpoint that returns an account's details along with a list of its associates, also accounts.
At this point I have the following:
models/account_relation.rb:
class AccountRelation <ApplicationRecord self.table_name ="mtom_account_relations" belongs_to :subject,foreign_key:"account_id",class_name:"Account" belongs_to :associate,foreign_key:"account_id",class_name:"Account" end
models/account.rb
class Account <ApplicationRecord self.table_name ="tab_accounts" self.primary_key ="account_id" ...
has_many:account_relations has_many :associates,:through => :account_relations has_many :subjects,:through => :account_relations end
controllers/account_controller.rb
class AccountsController <ApplicationController ...
def associates _account_id = params[:account_id]
@rs_account =Account .select("tab_accounts.account_id, tab_accounts.screen_name, tab_accounts.friends, tab_accounts.followers")
.where(:tab_accounts => {account_id:_account_id})
.as_json[0]
@rs_account['associates'] =Account.select("tab_accounts.account_id, tab_accounts.screen_name")
.joins(:subjects)
.where(:tab_accounts => {account_id:_account_id})
.as_json
render json:@rs_account end end
config/routes.rb:
Rails.application.routes.drawdo ...
get 'accounts/associates/:account_id',:to => "accounts#associates" end
When I run the method I get the following error:
PG::UndefinedColumn: ERROR: column mtom_account_relations.account_id does not exist LINE 1: ..._accounts" INNER JOIN "mtom_account_relations" ON "mtom_acco... ^ : SELECT tab_accounts.account_id, tab_accounts.screen_name FROM "tab_accounts" INNER JOIN "mtom_account_relations" ON "mtom_account_relations"."account_id" = "tab_accounts"."account_id" INNER JOIN "tab_accounts" "subjects_tab_accounts" ON "subjects_tab_accounts"."account_id" = "mtom_account_relations"."account_id" WHERE "tab_accounts"."account_id" = $1
1. I suspect the call to the non-existent table "subjects_tab_accounts" is being created from my .joins(:subjects) clause in the controller.
2. It thinks there's a "mtom_account_relations"."account_id" column.
I'd be grateful for any actionable assistance. Thank you for your attention.
- Joe
···
---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus