0

I have an integer in my Parent Object's table called completion_status with a default value of 0. Id like to set this to the same value as "@parent.child.count('completion', :distinct => true)", but I have no idea how to do this in the controller, or even if the controller is the best place to do this.

I know there's not much information included here, but let me know if I'm missing something important. I'm having kind of a brain-fart moment here.

EDIT: Just tried:

def set_completion 
   @app = App.find(params[:id]) 
   @app.update_attribute(:completion_status => @app.elements.count('completion', :distinct => true))
end
5
  • Set the value using that line and save it. There are a number of ways to do it, including that, using update_attribute, etc. Commented Jun 23, 2015 at 23:38
  • @DaveNewton I just tried out update_attribute but I get a wrong number of arguments (1 for 2) error. Check the edit. Commented Jun 23, 2015 at 23:49
  • Well, that's not how update_attribute works. Do you have access to any documentation, either locally (even ri) or on the Web? E.g., apidock.com/rails/ActiveRecord/Base/update_attribute Commented Jun 23, 2015 at 23:51
  • update_attribute expects 2 arguments: the name of the attribute and the vale you're updating it to eg: update_attribute(:quantity, 42) (which differs from update_attributes which takes name/value pairs as a hash) Commented Jun 23, 2015 at 23:52
  • 1
    @TarynEast I was using a hashrocket instead of a comma for some ungodly reason. Thank you for bringing me back to my senses. Now I only have to factor in the Child's Children's attribute as well and figure out how the hell I'm going to do this. Commented Jun 23, 2015 at 23:56

1 Answer 1

2

From what I understand, this value could possibly change everytime you create a new child, because creating a new child will change the value of @parent.child.count, so you want to reset completion_status everytime you create or update a new child, so one way to do this is inside the create and update action of the ChildrenController -assuming you have one :

def create 
  @parent = Parent.find(params[:parent_id]
  @child = @parent.children.build(child_params)
  if @child.save
    @parent.completion_status = @parent.children.count // this is the line to add
  end
end

This code could be improved in different ways but I'm just giving you an example.

Sign up to request clarification or add additional context in comments.

7 Comments

@AhmadAk-kheat I'll go ahead and see if I can adapt this for my use and I'll let you know. completion_status should only update when a Child is edited to change the boolean attribute of it from true to false (hence the ('completion', :distinct => true)). However, you are indeed right.
@AhmadAk-kheat Any idea how I'd count the Child's Children with true? When I try to I get an undefined method 'child's child'
@Nickdb93 why do you need to count the child's children? I thought a parent has multiple children not a child has multiple children.
An App (parent) has multiple Elements (child), each of which has multiple Features (child's children). Ultimately, I'd like completion status to equal ( Total Elements / Elements Completed ) + ( Total Features / Features Completed ). The end goal being to have a generally useful (though not mathematically exact) percentage that can be used in a progress bar.
@Nickdb93 Ok you can say this : @element.features.where(completed: true).count
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.