In rails, I know it is best to do a find through scope
e.g in questions_controller
def index @event = Event.find(params[event_id]) @question = @event.questions end
but is it also good practice to then do the same in the show action
def show @event = Event.find(params[:event_id]) @question = @event.questions.find(params[:id]) end
or better to just do a find straight on the Question model without scoping it through the event?
I am curious about that and also doing a similar thing with @question.comments.build vs Comment.new when making a new record without creating a question record at the same time.
thanks
As to your first question a few things to keep in mind:
Personally I would go for using Event’s interface for fetching the question(s) if merely to avoid premature optimization.
As for your second question, @question.comments.build will automatically set the right references in your new structure, so I’d go for that as long as @question is available. (Heck, I’d even consider making a @question.add_comment(..) method to the Question class).
Put in other words, premature optimization is the root of all evil. And loose coupling is good for you.