开发者

Collecting additional parameter Ruby on Rails

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 th开发者_JAVA技巧en call on the concept_comments table for all entries that have the proposal_id.

I am not even sure if that makes any sense.


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


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜