-1

I'm trying to make a formula in my project.rb model in my rails 4 app.

I have an attribute in an preference table, called delay. I want to calculate whether the tolerance on one user's part, is close to the delay required by another user.

In my project.rb, I've tried to do this as follows:

def publication_delay_variance
    if @current_user.profile.organisation.preference.delay >=  @project.profile.organisation.preference.delay
      'No problems here'
    elsif @current_user.profile.organisation.preference.delay * 90% >= @project.profile.organisation.preference.delay
      "Close, but not quite there"
    else   @current_user.profile.organisation.preference.delay * 50% >=  @project.profile.organisation.preference.delay

      "We're not in alignment here"
    end
  end

the current user is the current user who is currently logged in and interacting with the page. The other user is the user who created the project. Each user has an organisation. Each organisation has preferences. I'm trying to compare them.

Can anyone see what I'm doing wrong? I don't have much experience with this. My current attempt generates this error:

syntax error, unexpected >=
...ence.publication_delay * 90% >= @project.profile.organisatio...
..
Mel
  • 2,481
  • 26
  • 113
  • 273

2 Answers2

1

The problem is that 90% isn't valid in Ruby. You probably meant to use 0.9 instead. Also, your last else should be an elsif:

def publication_delay_variance
  if @current_user.profile.organisation.preference.delay >= @project.profile.organisation.preference.delay
    'No problems here'
  elsif @current_user.profile.organisation.preference.delay * 0.9 >= @project.profile.organisation.preference.delay
    "Close, but not quite there"
  elsif @current_user.profile.organisation.preference.delay * 0.5 >= @project.profile.organisation.preference.delay
    "We're not in alignment here"
  end
end

Of course, without an else you don't have a default case, so you should consider what behavior you want if none of those three conditions is true.

P.S. You can make this a lot more readable by assigning those values to local variables with shorter names:

def publication_delay_variance
  user_delay = @current_user.profile.organisation.preference.delay
  project_delay = @project.profile.organisation.preference.delay

  if user_delay >= project_delay
    "No problems here"
  elsif user_delay * 0.9 >= project_delay
    "Close, but not quite there"
  elsif user_delay * 0.5 >= project_delay
    "We're not in alignment here"
  end
end

P.P.S. 0.9 and 0.5 are magic numbers. Consider moving their values into constants.

Community
  • 1
  • 1
Jordan Running
  • 102,619
  • 17
  • 182
  • 182
  • Thanks very much for this. I've just realised that I can't use current_user in my project model. Going back to figure out how to get around that and then I'll come back and give this a try. – Mel Mar 12 '16 at 03:06
  • If this is an instance method on the User model then you could make it take a Project as an argument, so you would call it from your controller or view like `@current_user.publication_delay_variance(@project)`. – Jordan Running Mar 12 '16 at 03:11
  • I have got it in a method in my projects model. – Mel Mar 12 '16 at 03:13
  • Oh, well then you could do it the other way around and take a User as an argument, e.g. `@project.publication_delay_variance(@current_user)`. – Jordan Running Mar 12 '16 at 03:20
  • Thanks for that. I'll have to learn how to use it. I tried copying that line into my projects show action (and then making references to @current_user in the project.rb method, 'current_user' - but that's not correct. I'll try to learn how to use this and give it a try. Thanks again – Mel Mar 12 '16 at 03:46
1

In Ruby, % is the modulus operator, which takes two arguments x % y and returns the remainder of x / y. It doesn't make sense to have >= immediately after it, which is what the error message is telling you. To represent a percentage in Ruby, use a decimal number, eg 0.9.

kyle
  • 446
  • 4
  • 10