Note that there are some explanatory texts on larger screens.

plurals
  1. POCollections using has_many, :through relationships
    text
    copied!<p>I want the current user to read and purchase postings in clubs in which he is a member. I would love to do something like the following, but nothing I've read or tried has clicked yet. I will be using many nesting collections of this type, I'm stuck!<br /><br /> <strong>Models</strong></p> <pre><code> class User &lt; ActiveRecord::Base has_many :memberships has_many :clubs, :through =&gt; :memberships has_many :products class Membership &lt; ActiveRecord::Base belongs_to :user belongs_to :club class Club &lt; ActiveRecord::Base has_many :memberships has_many :users, :through =&gt; :memberships has_many :events class Event &lt; ActiveRecord::Base belongs_to :club class Product &lt; ActiveRecord::Base belongs_to :user has_many :posts class Post &lt; ActiveRecord::Base belongs_to :product belongs_to :event </code></pre> <p><strong>Posts Controller</strong></p> <pre><code>def index @user = current_user @posts_in_my_clubs = @user.memberships.collect { |a| a.products.posts} </code></pre> <p><strong>posts/index.html.erb</strong></p> <pre><code>@posts_in_my_clubs do |post| </code></pre> <p>Gives me the error:</p> <blockquote> <p>undefined method `memberships' for nil:NilClass</p> </blockquote> <p>To which I am not surprised. Additionally I want to be able to select only the posts that will be only at a certain event, etc. </p> <p>A summary of the models: Clubs have many Events &amp; Users. Users have many Products to which they can Post a quantity to sell at different Event dates. Only Users who are members of a Club can post or view posts in a Club. </p> <p>Instead of repeating data in the models, I have made associations, which seem to make the data inaccessible to creating collections in ActiveRecord. I've found in my research <a href="http://ar.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html" rel="nofollow">Association Join Models</a>, and <a href="http://metautonomo.us/projects/metawhere/#associations" rel="nofollow">Intelligent association mapping</a> with metawhere. These links make me feel like I'm getting close.</p> <p>Edit</p> <p>I'm signed in and can access all of the data I need as long as I'm not trying to do a collection like this. For instance I can check if I'm a member of the club by </p> <pre><code>def member?(club) return !!self.clubs.find(club) </code></pre> <p>I am a noob, so I must be missing something obvious here. Do I need to break up my collection into steps? i.e. collect user clubs, collect user clubs products, collect user clubs products posts... thanks</p> <p>Logger.info</p> <pre><code> @user = current_user logger.info @user logger.info 'super secret message' # @club_posts = @user.memberships.collect {|a| a.product.posts} </code></pre> <p>returns</p> <pre><code>#&lt;User:0x5b202a0&gt; super secret message </code></pre> <p>When I remove the comment, I get the nil error again, for 'product' now (progress!). I sure thought I was logged in before.. tho it was late</p>
 

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