Rails 4 has_many :through accessing field on join table -
i have 3 models:
class user < activerecord::base has_many :user_leave_portfolios has_many :leave_portfolios, :through => :user_leave_portfolios end class leaveportfolio < activerecord::base has_many :user_leave_portfolios, dependent: :destroy has_many :users, :through => :user_leave_portfolios end class userleaveportfolio < activerecord::base belongs_to :user belongs_to :leave_portfolio # table has additional field named 'leave_amount' end
the 'leave_amount' field on user_leave_portfolio table used user chosen leave amount based on leave_portfolio
according documentation , various other blogs , articles, should able access 'leave amount' field with:
u = user.first u.leave_portfolios.first.leave_amount
however, following error:
nomethoderror: undefined method `leave_amount' #leaveportfolio:0x007f8708e3dbe0
schema:
create_table "leave_portfolios", force: :cascade |t| t.string "name", limit: 255 t.integer "calculation_type", limit: 4 t.integer "period", limit: 4 t.integer "default_amount", limit: 4 t.integer "organisation_id", limit: 4 t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "user_leave_portfolios", force: :cascade |t| t.integer "leave_portfolio_id", limit: 4 t.integer "user_id", limit: 4 t.float "leave_amount", limit: 24 t.datetime "created_at", null: false t.datetime "updated_at", null: false end create_table "users", force: :cascade |t| t.string "email", limit: 255, default: "", null: false t.string "encrypted_password", . . . end
you should do
u = user.first u.user_leave_portfolios.first.leave_amount
as have leave_amount
in user_leave_portfolios
not in leave_portfolios
.
Comments
Post a Comment