Greetings,
This is something that has completely stumped my development processes
on my new application. The application helps members exchange feedback
on their stories (for creative writers). With that in mind, I’m trying
to create associations between the different tables:
class Member < ActiveRecord::Base
has_many :stories
has_many :critiques
end
class Story < ActiveRecord::Base
has_many :critiques
belongs_to :member # foreign key => member_id
end
class Critique < ActiveRecord::Base
belongs_to :story # foreign key => story_id
belongs_to :member # foreign key => member_id
end
I’m not entirely sure, though I’m under the impression that as long as
you set up the associations correct and add the correct columns
(foreign keys), Rails will use its magic. For example, if I create a
new story, then the member_id column would automatically be updated
with the active member’s id. Unfortunately, this doesn’t happen in my
application. Thus, I’m either doing something wrong (very possible),
or Rails doesn’t do this automatically.
Is the above code correct? If so, does Rails populate the foreign key
column automatically? If so… what am I doing wrong?
My next question has to deal with what you can do with these
associations. Lets say I wanted to display within my index.html.erb
file a list of stories (links) and for each story, a link to each of
their critiques. How would the code in my controller look? And how
would I structure my view code?
I’ve read over a lot of the API documentation, the rails way, the
agile development book, and many posts here. It’s just not clicking.
So, any help would be very much appreciated. Thank you for your time
and assistance!
~Dustin T.