samedi 20 mai 2017

Joining two or more tables in Rails?

I have 3 tables Ledgers, Accounts, Users and organisation

I am trying to get accounts using ledger Id for each specific user.

Table Ledgers contains - LedgerID, Organisation ID
Table Accounts contains  - AccountID, AccountName, LedgerID
Table Users contains - UserID, name
Table Organisation contains - OrganisationId, UserID, organisation name

Heres my models.

class Accounts < ActiveRecord::Base
belongs_to :ledger

class Ledger < ActiveRecord::Base
has_many :accounts
belongs_to :organisation

class User < ActiveRecord::Base
has_many :organisations

Here is what i have tried.

def show
 if authenticated_user
 @Usersorganisations = @authenticated_user.organisations
  /This gets the user's organisations/

 @ledger_id = Ledger.where("organisation_id = ?", @Usersorganisations.pluck(:id))

 render json: @ledger_id.as_json
end

But trying this gives me PG::DatatypeMismatch: ERROR:

Aucun commentaire:

Enregistrer un commentaire