質問

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
役に立ちましたか?

解決

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)

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top