ruby on rails - How to setup multiple has_many through relationships in Rails3 dependent on field in joining table -


this simple, i'm still sorta beginning rails , can't seem figure out right phrase type google find answer.

i've got pretty simple has_many through relationship described below:

user
-user_id
-name

article
- article_id
- title

article_relationship
- user_id
- article_id
- relationship_type

on relationship type, string or int represent type of relations, favorite, recently_viewed, written_by, ect. how setup multiple has_many :articles, :through => :article_relationships can access articles of specific type of relationship through user.recently_viewed, user.favorite, ect?

thanks bunch.

you're on right track need make use of scopes:

class article < activerecord::base   # here recently_viewed_type refers type of relationship, whatever   # constant defined as.   scope :recently_viewed,     where('article_relationships.relationship_type=?', recently_viewed_type) end 

then can access user directly:

@user.articles.recently_viewed.all 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -