views:

71

answers:

2

I am building a project management app and need some help with how to pass a parameter (I think that is how you say it). Here is what I have going on.

I have a table called "proposals" and "proposals" allow you to create multiple concepts per proposal. I have a table called "concepts" and on each "concept" the user can "comment". The way that I have set this up is to generate a table called concept_comments.

Currently the comments are only associated with the concepts but I would like to display all the comment, across all the concepts, for that particular proposal. I am guessing that his has to do with two things:

  1. including another line to collect proposal_id when creating a comment.
  2. Assigning a has_many :concept_comments to the model/proposal.rb file.
  3. Adding map.resources :proposals, :has_many => :concept_comments.

Not sure if that is correct but that is in my head. Only thing I have done so far is to create a column in my concept_comments table called proposal_id. Here is my concept_comments_controller.rb code for 'create':

  def create
    @concept = Concept.find(params[:concept_id])
    @concept_comment = @concept.concept_comments.create!(params[:concept_comment])
    respond_to do |format|
      format.html { redirect_to @concept }
      format.js
    end
  end

Not quite sure how to tell it to also collect the proposal_id. Somehow I need to tell it to look at the concept_id that has been passed in, then pull the proposal_id number from the concept table, and pass that to the proposal_id in the concept_comments table.

My thinking is that I can then call on the concept_comments table for all entries that have the proposal_id.

I am not even sure if that makes any sense.

A: 

Couldn't you just iterate over all of the given proposal's concepts and gather up their comments into one big array, or is this something that's going to be happening millions of times a second?

Azeem.Butt
As you can probably tell I am new to all of this. Definitely won't be happening millions of times...ever. I just have never done any of this before...how would I go about implementing what you mention above?
bgadoci
You should learn Ruby.
Azeem.Butt
+1  A: 

well - you could pass in the proposal id, but, if you already have a concept_comment id, you don't need a proposal id

@proposal_comments = ConceptComment.all(:joins => :concept, 
                                        :conditions => ["concepts.proposal_id = ?", 
                                        @concept_comment.concept.proposal_id])

Where @concept_comment is on a member action of the comments controller - for collection actions, you will need to pass in a proposal id and substitute that in for @concept_comment.concept.proposal_id

Omar Qureshi
Is the question mark part of that code or is that something I am supposed to put in there? Also, I can see conceptually what you are talking about but the vernacular is what kills me since I am new. Are you saying that if I place this code in my controller I can then just reference @proposal_comments from somewhere?
bgadoci
yes, the question mark is a bind variable, e.g. :conditions => ["foo = ?", bar] is equivalent to :conditions => "foo = #{bar}" however bar is escaped.If you put that in your controller you can access @proposal_comments from your view and from your controller after you have defined it.
Omar Qureshi