user1096557
user1096557

Reputation:

How to resolve a rails model attribute and gem name conflict?

On Rails 3.2.1. I have a model called Car with an attribute called rate (indicates a price quantity). I'm using the ajaxful_rating gem which associates a method called rate (to submit a rating of how good a teacher is) with a Car.

So in my view, when I'm trying to show a price quantity with:

<%= @car.rate %>

I get a syntax error because there's naming confusion. How do I let Rails know that I want to call the rate attribute and not the rate method? Is there an easy way to resolve this without rolling back my DB and renaming the attribute?

Upvotes: 3

Views: 1007

Answers (2)

Dogweather
Dogweather

Reputation: 16819

Well, first of all, you wouldn't have to roll back the database to rename the attribute. You would just create a new migration with this one change: How can I rename a database column in a Ruby on Rails migration? Those kinds of changes are necessary from time to time and so Rails has this mechanism for handling them.

But to answer your question, an alternate workaround would be to write a new method on car which actually gives access to the attribute like this:

def rate_price
  read_attribute(:rate)
end

Upvotes: 1

Andrew Marshall
Andrew Marshall

Reputation: 97004

You can use read_attribute:

@car.read_attribute(:rate)

but I suggest that you resolve the conflict by renaming one of them as it will just cause you problems and confusion down the road.

You don't need to rollback your database to rename a column, you just have to create a new migration that does so.

Upvotes: 1

Related Questions