Farhan wrote:
Hi,
I’m a total noob to rails. I’m working my way through my first app and
I desperately need help with through associations in rails.
Through associations can be a bit complex for a beginner. Are you sure
you don’t want to start with something simpler? Have you worked through
the tutorial in AWDWR yet?
Task: get number of unread articles.
Database structure:
A. Users
has_many :activity
has_many :article, :through => :activity
Well, for starters, you should always use plurals with has_many
relationships. So, “has_many :activities, has_many :articles, :through
=> :activities”, etc. AWDWR would have taught you this, and many other
fundamental concepts.
B. Activity
belongs_to :user
belongs_to :article
good. But I’m not sure I understand the purpose of this model. What’s
an “activity” as it relates to a user and an article?
C. Articles (user_id,feed_id,is_read)
has_many :reading
has_many :user, :through => :reading
belongs_to :feed
Again, “:has_many :readings”
Any article that is in this table has been read and the “is_read”
field has been set to true.
What table? Articles? I think you forgot to post a description of
your “Reading” model.
D. Feeds
has_many :article
:articles
How do I go about getting the number of unread articles for a given
user and feed?
Not sure I can really help here without a better understanding of the
purpose of the Activity model and the structure of your Reading model.
Any help will be greatly appreciated.
Thanks in advance.
Best,
Farhan
–
http://www.5valleys.com/