1

I have a Rails 3 Active Record finder method and i am trying update it to rails 4 patten.

In Rails 3, my code looked like this

StripeEvent.setup do
  subscribe 'customer.subscription.deleted' do |event|
    user = User.find_by_customer_id(event.data.object.customer)
    user.expire
  end
end

In Rails 4, i tried this, is this piece of code correct?

StripeEvent.setup do
  subscribe 'customer.subscription.deleted' do |event|
    user = User.where(customer_id: (event.data.object.customer) )
    user.expire
  end
end
Benjamin
  • 2,108
  • 2
  • 26
  • 46

2 Answers2

1

It am unclear why you choose to fetch records in this way when you can do

user = User.find_by_customer_id(event.data.object.customer)
Dmitry Matveev
  • 5,320
  • 1
  • 32
  • 43
  • So does rails 4 still support this find_by_customer_id ? – Benjamin Sep 28 '13 at 20:36
  • Yes, Dynamic attribute-based finders [http://api.rubyonrails.org/](http://api.rubyonrails.org/) are still supported. You can use either the form I have mentioned or the following User.find_by(customer_id: event.data.object.customer) – Dmitry Matveev Sep 28 '13 at 21:45
  • please consider accepting the answer if you are satisfied with it or ask for more details – Dmitry Matveev Sep 28 '13 at 22:30
-1

The find_by_* is deprecated.

In Rails 4, please use find_by like so:

User.find_by(customer_id: event.data.object.customer)

You can read more about Deprecated Finders in this great blog post: What's new in Active Record [Rails 4 Countdown to 2013] | The Remarkable Labs Blog. Or checkout CodeSchool's Rails 4: Zombie Outlaws course.

Juanito Fatas
  • 9,419
  • 9
  • 46
  • 70
  • This is incorrect. The only deprecated finders are `find_all_by_...` `find_last_by_...` `find_or_create_by_...` `find_or_initialize_by_...` `scoped_by_...` – Michael Dec 14 '15 at 17:29