Search code examples
activerecordruby-on-rails-4dynamic-finders

Rails 4 Active Record finder method


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

Solution

  • 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)