# Has\_many & belongs\_to with Plugins (login\_engine)

**URL:** <https://www.ruby-forum.com/t/has-many-belongs-to-with-plugins-login-engine/51340>\
**Category:** Rails\
**Created:** [December 29, 2005, 10:16pm UTC](https://www.ruby-forum.com/t/has-many-belongs-to-with-plugins-login-engine/51340 "2005-12-29T22:16:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vaishal\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a3d4f5/32.png) [@Vaishal\_S](https://www.ruby-forum.com/u/Vaishal_S)\
**Post date:** [December 29, 2005, 10:16pm UTC](https://www.ruby-forum.com/t/has-many-belongs-to-with-plugins-login-engine/51340/1 "2005-12-29T22:16:18Z")

</div>

Hello list,

Probably a newbie question but couldn’t find the answer in the archives.

I am using the login\_engine Plugin to implement the login functionality.  
I  
have a JournalEntry model that interacts with the journal\_entries table.  
I  
would like to link the journal entries to the user who entered it.  
Essentially, it is a one to many relationship between the model  
User(from  
login\_engine) and my JournalEntry model.

My database has a users table (resulting from a standard implementation  
of  
the login\_engine) and the journal\_entries table has a user\_id field and  
a  
foreign key constraint to the id field in the users table.

To specify the relationship between the two models, in the JournalEntry  
model, I have  
class JournalEntry \< ActiveRecord::Base  
belongs\_to :user

And to specify the has\_many relationship, I figured I would have to  
override  
the User model in the login\_engine. Hence I copied the user.rb from the  
/vendor/plugins/login\_engine/app/models to /app/models and  
modified it as below  
class User \< ActiveRecord::Base  
has\_many :journal\_entries  
include LoginEngine::AuthenticatedUser  
…

Now my limited undertanding tells me that after reloading the app, if I  
login and create a new journal entry, the user\_id colum should be  
populated  
with the user\_id of the user that created the entry but it’s not doing  
that.  
Am I missing something? Do I have to specify it explicitly somewhere?  
Or,  
since the user model is in a plugin, do I have to specify it some other  
way  
like belongs\_to :LoginEngine:User (I know the syntax is wrong)?

Thanks in advance for any help,  
Vaishal

---

<div class="post-metadata">

**Author:** ![Vaishal\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a3d4f5/32.png) [@Vaishal\_S](https://www.ruby-forum.com/u/Vaishal_S)\
**Post date:** [December 29, 2005, 10:40pm UTC](https://www.ruby-forum.com/t/has-many-belongs-to-with-plugins-login-engine/51340/2 "2005-12-29T22:40:29Z")

</div>

I found the answer at  
[http://rails.techno-weenie.net/question/2005/12/14/avoiding\_hidden\_field](http://rails.techno-weenie.net/question/2005/12/14/avoiding_hidden_field)

My understanding was wrong. Rails wouldn’t automatically enter the  
values  
upon record save just because I have specified the belongs\_to and  
has\_many  
relationship. It has to be explicitly done in the code.

So then, why are relationships useful in my case? Just for retrieval  
using  
the find()?

On 12/29/05, Vaishal S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> H

ello list,

---

<div class="post-metadata">

**Author:** ![Vaishal\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a3d4f5/32.png) [@Vaishal\_S](https://www.ruby-forum.com/u/Vaishal_S)\
**Post date:** [December 29, 2005, 11:22pm UTC](https://www.ruby-forum.com/t/has-many-belongs-to-with-plugins-login-engine/51340/3 "2005-12-29T23:22:36Z")

</div>

For any time where you are in or have an instance of model A, and wish  
to access the corresponding objects of Model B. I don’t find this to be  
a trivial case.

Regarding your example, the system has no way of knowing that you want  
the user column to be filled in with the currently logged in user. It  
seems obvious to you, but the system can’t know that. Extend your  
example to a relationship that doesn’t involve user, and your issue is  
no longer applicable, yet the relationship constructs are enormously  
useful.
