← Back to Upcase

Week 3 - extend ActiveModel::Naming not working


(David Becerra) #1

I just got to the part where I’m creating the timeline class and i’m trying to implement ActiveModel Naming, but it’s not working. I get the same “to_partial_path” error. When I create the “to_partial_path” method and specify a partial, it works as expected and I’m able to continue with the workshop.

I’m going to continue using the to_partial_path method, but I’d really like to get the ActiveModel naming thing working.

Here’s my code:

app/models/timeline.rb

class Timeline
  extend ActiveModel::Naming

  def initialize(user)
    @user = user
  end
end

I saw another thread in here where they had to use ActiveModel::Conversion, but that didn’t work for me either.

The only thing that worked for me involves removing the ActiveModel::Naming extension and using this instead.

def to_partial_path
  "timelines/timeline"
end

Please help!

Thnx!


(David Becerra) #2

I got this to work with include ActiveModel::Model instead of extend ActiveModel::Naming According the the API docs, this includes more than just naming.

Is this okay to do? I’m not quite sure what the pros and cons are here.


(Patrik Bóna) #3

Which rails version do you use? In rails 4 include ActiveModel::Conversion should work without any issue.


(David Becerra) #4

I’m using rails 4. Ah yes, include not extend That worked for me! Thank you :smile:


(Patrik Bóna) #5

Yeah I did the same thing. I’ve used extend as first too ;).


(Kevin Lozandier) #6

For Rails 4 work that involves what you’re trying to do, it’s best to use ActiveModel::Model. It’s now best practice to do so.

It gives you :naming, :conversion, and essentially all the common things from ActiveModel required for robust classes dealing with Rails without necessarily being attached to anything that’ll be written in a database.


(David Becerra) #7

Very cool! Thanks, @kevinlozandier!