Map attribute to object ID

20 views
Skip to first unread message

mrmanishs

unread,
Apr 7, 2015, 5:51:00 PM4/7/15
to rubyonra...@googlegroups.com
Hello there,

I would like to map an attribute to an object ID. How would I go about doing this?

Example:

A User  ActiveRecord model exists with an id field

A Message ActiveRecord model exists with id, from_user_id, to_user_id

I would like from_user_id to map to user.id and to_user_id to map to user.id.

What's the best way to accomplish this?

Manish

Hassan Schroeder

unread,
Apr 7, 2015, 6:37:45 PM4/7/15
to rubyonrails-talk
On Tue, Apr 7, 2015 at 2:50 PM, mrmanishs <man...@acceleweb.com> wrote:

> I would like to map an attribute to an object ID.

> Example:
>
> A User ActiveRecord model exists with an id field

An ActiveRecord "id" is not the same as an "object id" in Ruby, so I'm
not sure I understand what you want. However,

> A Message ActiveRecord model exists with id, from_user_id, to_user_id
>
> I would like from_user_id to map to user.id and to_user_id to map to
> user.id.
>
> What's the best way to accomplish this?

Create it that way? e.g.

Message.create(from_user_id: 1, to_user_id: 2, text: '...' )

HTH, apologies if I've misunderstood your goal,
--
Hassan Schroeder ------------------------ hassan.s...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan
Consulting Availability : Silicon Valley or remote

Colin Law

unread,
Apr 8, 2015, 3:56:09 AM4/8/15
to rubyonra...@googlegroups.com
If you are not already doing so you should use activerecord relations
to help with this. I suspect that maybe you are a beginner, in which
case work right through a good tutorial such as railstutorial.org
(which is free to use online) which will show you the basics of rails.


Colin
Reply all
Reply to author
Forward
0 new messages