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?
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?
Thanks for replying. I'm will be getting the AWDWR book this evening.
Okay; so I made the changes to pluralize the associations for
has_many. Thanks for the tip; makes the code more readable.
I actually made a couple of typos in the "Database Structure". I had
changed the name of the "readings" to "activity" (now "activities").
The new database structure is as follows with the changes you
requested:
The activity model will keep track of all read articles for the user.
That is when a user reads an article it gets added to the "activities"
table. This table has the user_id, article_id, and the is_read
columns. Any article that is added to the activity model has the
is_read column set to "true".
Hope this helps; otherwise let me know and I will provide more
information.
Thanks for replying. I'm will be getting the AWDWR book this evening.
Okay; so I made the changes to pluralize the associations for
has_many. Thanks for the tip; makes the code more readable.
I actually made a couple of typos in the "Database Structure". I had
changed the name of the "readings" to "activity" (now "activities").
The new database structure is as follows with the changes you
requested:
The activity model will keep track of all read articles for the user.
That is when a user reads an article it gets added to the "activities"
table. This table has the user_id, article_id, and the is_read
columns. Any article that is added to the activity model has the
is_read column set to "true".
Hope this helps; otherwise let me know and I will provide more
information.
Thanks,
Farhan
Alright, first... at B. above, you say the activity table has a
"feed_id" column, but below you say it's "article_id". I 'm going to
plow forward with the second and ignore the first, since that what it
should be.
Ok, so now we want to get all the *read* articles for a particular user.
Add something like this to your User class.
has_many :readings, :class_name => 'Activity', :conditions => 'is_read = 1'
has_many :read_articles, :through => :readings