Ephraim,

If you want to get all the :microposts for a :selection you have to define 
the association in that direction.

You have Micropost belongs_to :selection, but you don't have Selection 
has_many :microposts.  You don't automatically get a two-way association by 
just defining one end.  So add has_many :microposts to Selection.

You don't show your User model, but I suspect the issue with u.microposts 
is the same -- you have to add User has_many :microposts, since you have 
Micropost belongs_to :user.

Let us know if that fixes it.

Cheers,

Chris

On Wednesday, August 7, 2013 3:10:09 PM UTC-7, Ephraim Feig wrote:
>
>  I have an application that creates microposts under categories and 
> subcategories (which I call selections). The user can write in two fields, 
> the category field and the selection field. My models are:
>
>
> class Micropost < ActiveRecord::Base
>   attr_accessible :content, :category_content, :selection_content, 
> :category_id, :selection_id, :user_id
>
>   belongs_to :user
>   belongs_to :category
>   belongs_to :selection
>
>   validates :user_id, presence: true
>   validates :category_content, presence: true
>   validates :selection_content, presence: true
>
>
>   default_scope order: 'microposts.created_at DESC'
>
> end
>
>
> class Category < ActiveRecord::Base
>   attr_accessible :content, :id, :count, :c_count
>   has_many :selections
>   has_many :comments
>
>   default_scope order: 'categories.count DESC'
>
> end
>
>
> class Selection < ActiveRecord::Base
>   attr_accessible :content, :count, :category_id
>   belongs_to :category
>
>   default_scope order: 'selections.count DESC'
>
> end
>
>
> In my console, when I create a user u and enter "u.microposts" I get the 
> desired response (a list of microposts created by u). When I create a 
> category c and enter 
>
> "c.selections" I also get the desired response (a list of selections that 
> belong to c). But when I enter "s.microposts" I get the following error 
> message- "undefined 
>
> method `microposts' for #<Selection:0x57c6b40>". Similarly, when I enter 
> "u.microposts" I get the error message- "undefined method `microposts' for 
>
> #<Category:0x45c0498>".  Any ideas why this is happening?
>
> I am developing on Windows using RubyMine. I am definitely moving to OS X 
> soon, because I have to make too many hoops working around the Windows 
> environment, but I don't see why this would make a difference here. 
>

-- 
-- 
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby
--- 
You received this message because you are subscribed to the Google Groups "SD 
Ruby" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to