Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3: Get attributes between associated tables and how to call those in Model classes
    primarykey
    data
    text
    <p>I have 4 tables leagues, divisions, teams and coaches. The relationships between these tables described as a league has many divisions, a division has many teams but only one coach for a team. I also want to query leagues from team through a division which associated to the team.</p> <p>My current setup:</p> <pre><code>class League &lt; ActiveRecord::Base has_many :divisions has_many :teams, :through =&gt; :divisions, :readonly =&gt; true has_many :coaches, :through =&gt; :teams, :readonly =&gt; true end class Division &lt; ActiveRecord::Base belongs_to :league has_many :teams end </code></pre> <p>I should mention right this point that I am using paperclip to upload team logos. However, when I wanted to change the storage path as: /public/assets/images/teams/[division_name]/:style/:basename.:extension, I don't know how to get the division name of the team to use for the path &amp; url of file system storage.</p> <pre><code>class Team &lt; ActiveRecord::Base belongs_to :division belongs_to :league belongs_to :stadium has_many :players has_one :coach has_attached_file :logo, :styles =&gt; { :large =&gt; "120x180&gt;", :thumb =&gt; "100x100&gt;", :tiny =&gt; "25x25&gt;" }, :url =&gt; "/assets/images/teams/#{name}/:style/:basename.:extension", :path =&gt; ":rails_root/public/assets/images/teams/#{name}/:style/:basename.:extension" end class Coach &lt; ActiveRecord::Base belongs_to :division belongs_to :league belongs_to :stadium belongs_to :team end </code></pre> <p>My problems so far:</p> <ol> <li>Am I setting things correctly? It's very important that I want to query coaches and teams in a league or a division.</li> <li>In Team class, I want to define a method(or whatever it called) which returns the division name of the team to include that division name to the url &amp; path of storage configuration as above.</li> </ol> <p>Any help would be much appreciated!</p> <p><strong>UPDATE:</strong> I tried to use <code>self.division.name</code> in team.rb class to get the name of division of the team:</p> <pre><code>has_attached_file :logo, :styles =&gt; { :large =&gt; "120x180&gt;", :thumb =&gt; "100x100&gt;", :tiny =&gt; "25x25&gt;" }, :url =&gt; "/assets/images/teams/#{self.division.name}/:style/:basename.:extension", :path =&gt; ":rails_root/public/assets/images/teams/#{self.division.name}/:style/:basename.:extension" </code></pre> <p>However, I am getting this error message:</p> <pre><code>`method_missing': undefined method `division' for #&lt;Class:0x10c6a2900&gt; (NoMethodError) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload