rails ActiveRecord join tables cascading belongs_to -


given following simple associations, , selected_company, query correctly written documents in selected company ?

    class document < activerecord::base             belongs_to :user     end      class user < activerecord::base       belongs_to :company       has_many :documents     end      class company < activerecord::base         has_many :users     end  === query     selected_company: company.first     documents_in_selected_company = document.joins(:user).joins(:user => :company).where(:company_id => selected_company[:id) 

thanks feedback

just 2 small fixes , association names should lowercase ... , should contains query string :

    documents_in_selected_company = document.joins(:user).joins(:user => :company).where("companies.id = ?", company.id) 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -