Rails associations / sessions question
I am working on a app for my kid开发者_Go百科s to log their chores. I have 3 children (Nick, Siena, Mac) and have a home page with each name hyperlinked ...
I have the following associations:
Child:
has_many :completions
has_many :chores, :through=>:completion
Completion:
belongs_to :child
belongs_to :chore
Chore:
has_many :completions
has_many :kid, :through=>:completion
How do I (upon clicking the child's name) save the child_id as a session object for posting completions to the completions models?
How do I clear / change that sesison to a new child when another child clicks their name in the homepage?
Any help is greatly appreciated. Thanks, CB
So josh went above and beyond. As a noob i was asking something much more simple and elementary for most folks. The answer was quite simple:
ApplicationController
private
def current_child
child = Child.find(params[:id])
session[:child_id] = child.id
end
end
This allowed me to store that child's id in a session and post to the completed model.
If I understand the question correctly (at least as described in your comment), I did something similar recently. See Building a nested attribute from multiple parent objects. Basically, I used polymorphic_url to make a link to create a new item (I would probably use @child.chores.build(params)) and pass the attribute :chore_id, i.e.
link_to "Mark as complete", polymorphic_url([:new, @child, :completion], :chore_id => @chore.id)
In your controller make sure that for your ChoresController#new you have something like
def new
@chore = <current_child>.chores.build(params)
end
Hope this helps.
精彩评论