0

別のモデルのモデルの 1 つの特定のテーブル行を取得しようとしていますが、問題があります

class Volunteer < ActiveRecord::Base
  belongs_to :volunteerable, polymorphic: true

  def item_quantity_have
    a = Stuff.first
    a.quantity_have
  end

  def item_quantity_given
    self.quantity_given
  end

  def add_em_up
    item_quantity_have + item_quantity_given
  end

  def make_it_happen
    a = Stuff.first
    a.update_attributes(quantity_have: add_em_up)
  end
end

これは私がこれまでに持っていたものであり、機能していますが、STUFFの最初のモデルに投稿するだけです。

Stuff.first 

しかし、投稿する必要がある特定のIDを見つけるのに苦労しています....私はコントローラーでできることを知っています

params[:id]

しかし、モデルではそれができないと思います....では、コントローラーで行うように、モデルで適切なパラメーターを見つけるにはどうすればよいですか?

次のようなことができるようになりたい

a = Stuff.find_by_id(:id)
a.quantity_have 

その特定のIDのその番号を取得します

4

0 に答える 0