# Adding to objects returned by AR

**URL:** <https://www.ruby-forum.com/t/adding-to-objects-returned-by-ar/79095>\
**Category:** Rails\
**Created:** [December 12, 2006, 3:08am UTC](https://www.ruby-forum.com/t/adding-to-objects-returned-by-ar/79095 "2006-12-12T03:08:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![will](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@will](https://www.ruby-forum.com/u/will)\
**Post date:** [December 12, 2006, 3:08am UTC](https://www.ruby-forum.com/t/adding-to-objects-returned-by-ar/79095/1 "2006-12-12T03:08:37Z")

</div>

Ahoy,

I have a location object that looks something like this when using  
\<%= debug @location %\>

— !ruby/object:Location  
attributes:  
title: The Studio  
id: “3”  
zip\_id: “40915”  
address: 9013 Main Street

I want to add a another method, “godzilla”

— !ruby/object:Location  
attributes:  
title: The Studio  
id: “3”  
zip\_id: “40915”  
address: 9013 Main Street  
godzilla: “raaaaaaaa”

How do i do this?

---

<div class="post-metadata">

**Author:** ![will](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@will](https://www.ruby-forum.com/u/will)\
**Post date:** [December 12, 2006, 5:39am UTC](https://www.ruby-forum.com/t/adding-to-objects-returned-by-ar/79095/2 "2006-12-12T05:39:15Z")

</div>

In your “location” model,

attr\_accessor :godzilla

Is that what you’re asking?

Jason

---

<div class="post-metadata">

**Author:** ![will](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@will](https://www.ruby-forum.com/u/will)\
**Post date:** [December 12, 2006, 7:54am UTC](https://www.ruby-forum.com/t/adding-to-objects-returned-by-ar/79095/3 "2006-12-12T07:54:47Z")

</div>

Jason,

Then :godzilla is nil by default.  
attr\_accessor :godzilla  
allows me to do in the controller

@godzilla = “asdf”

then

location.godzilla will return “asdf”

but how do i set that as a default in the model?

> In your “location” model,
> 
> attr\_accessor :godzilla
> 
> Is that what you’re asking?
> 
> Jason

---

<div class="post-metadata">

**Author:** ![will](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@will](https://www.ruby-forum.com/u/will)\
**Post date:** [December 13, 2006, 3:35am UTC](https://www.ruby-forum.com/t/adding-to-objects-returned-by-ar/79095/4 "2006-12-13T03:35:29Z")

</div>

Chris H wrote:

> Hello,
> 
> I store sessions in my database and I’d like to store a session\_id  
> with a user when they log in. How do I determine what a user’s session  
> id is?

session.session\_id

> The reason I want to do this is because I’d like to display users  
> currently logged in.

session.session\_id won’t help here as it applies to the current user  
only.

> If I can link a session to a user in my database, then I can check the  
> updated\_at column  
> in the sessions table and determine who is active.
> 
> Is this the correct way to go about doing this?

There was a similar discussion about this previously. Basically you  
already  
have access to the user session in the AR Store. You just need a way to  
differentiate a user session from anonymous ones, if you have both  
session  
types.

It might be possible to add a user\_id column to the AR Store table and  
populate the column when a user logs in. Alternatively, record users in  
a  
separate user\_table. You will have more flexibility in storing and  
managing  
user info with this approach.

HTH,

Long  
www.edgesoft.ca/blog/read/2 - rails articles

---

<div class="post-metadata">

**Author:** ![will](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@will](https://www.ruby-forum.com/u/will)\
**Post date:** [December 13, 2006, 2:57am UTC](https://www.ruby-forum.com/t/adding-to-objects-returned-by-ar/79095/5 "2006-12-13T02:57:27Z")

</div>

Hello,

I store sessions in my database and I’d like to store a session\_id  
with a user when they log in. How do I determine what a user’s session  
id is?

The reason I want to do this is because I’d like to display users  
currently logged in.

If I can link a session to a user in my database, then I can check the  
updated\_at column  
in the sessions table and determine who is active.

Is this the correct way to go about doing this?

thanks in advance,  
Chris.

---

<div class="post-metadata">

**Author:** ![will](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@will](https://www.ruby-forum.com/u/will)\
**Post date:** [December 13, 2006, 8:01pm UTC](https://www.ruby-forum.com/t/adding-to-objects-returned-by-ar/79095/6 "2006-12-13T20:01:16Z")

</div>

take a look at this for some info:

[http://habtm.com/articles/2005/12/15/whos-online](http://habtm.com/articles/2005/12/15/whos-online)

---

<div class="post-metadata">

**Author:** ![will](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/90ced4/32.png) [@will](https://www.ruby-forum.com/u/will)\
**Post date:** [December 13, 2006, 3:41am UTC](https://www.ruby-forum.com/t/adding-to-objects-returned-by-ar/79095/7 "2006-12-13T03:41:32Z")

</div>

Hi Chris,

Chris H wrote:

> How do I determine what a user’s session id is?
> 
> If I can link a session to a user in my database, then  
> I can check the updated\_at column in the sessions  
> table and determine who is active.

session[:id] will give you the id of the current session, but I don’t  
think  
that’s what you’ll end up wanting.

hth,  
Bill
