0

In my Rails model I have a description method:

def description
  if self.channels.nil?
    return self.allowed_ip_addresses
  else
    return self.channels.to_s + ' channels - ' + self.allowed_ip_addresses
  end
end

I have quite a few models where I created a description method which returns some useful info based on the other properties of that model.

The problem I have just realised is that this model in particular has a description column in it's table so I was just wondering the best way to solve this, I've looked around but not found anything useful.

The first thing I thought of was to pass an argument when calling description, e.g.

def description(table_value = 0)
  if table_value
    return self.description
  end

  if self.channels.nil?
    return self.allowed_ip_addresses
  else
    return self.channels.to_s + ' channels - ' + self.allowed_ip_addresses
  end
end

but soon realised that this is pointless since it will just call the description method recursively and die (assuming a 1 is passed with the call to description).

I could also change the table column name but it's used elsewhere so I'm just trying to see if there is an alternative solution.

1
  • Use read_attribute method from within the model to force it to get the DB method. You can also use super. Commented Oct 28, 2013 at 12:08

2 Answers 2

2

I would definitely rename your custom description method.

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

3 Comments

or the column, whichever is easier or more appropriate for consistency in your application
I agree, that would probably be the best thing to do but I only need to do the override in one place, compared to the amount of changes I'd need to make if I renamed the column or custom description method.
@martincarlin87 I think renaming is worth putting an effort in it. If you do more hacks like this, your code will soon look like crap.
0

Ruby has the concept of "super" which lets you call methods in parent classes or modules. In Rails, Active Record lets you take advantage of this by overriding the "column method" and still get to the database value by calling super. It's not really clear from your question under what conditions you want to use the database value and when you want to use the custom logic. Here's some pseudo code to get you started though:

def description
  return super if Use Database Value?

  if self.channels.nil?
    return self.allowed_ip_addresses
  else
    return self.channels.to_s + ' channels - ' + self.allowed_ip_addresses
  end
end

3 Comments

ah, perfect, thank you very much. I was hoping there'd be something Rails-y I could do but just couldn't find it.
@martincarlin87 if this works for you, please accept the answer so others know your question has been answered.
yip, I always accept, just that you were so quick with the answer I had to wait a few minutes (think answers can only be accepted after 7 minutes)

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.