Has_many & belongs_to with Plugins (login_engine)

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.
have a JournalEntry model that interacts with the journal_entries table.
would like to link the journal entries to the user who entered it.
Essentially, it is a one to many relationship between the model
login_engine) and my JournalEntry model.

My database has a users table (resulting from a standard implementation
the login_engine) and the journal_entries table has a user_id field and
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
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
with the user_id of the user that created the entry but it’s not doing
Am I missing something? Do I have to specify it explicitly somewhere?
since the user model is in a plugin, do I have to specify it some other
like belongs_to :LoginEngine:User (I know the syntax is wrong)?

Thanks in advance for any help,

I found the answer at

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

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

On 12/29/05, Vaishal S. [email protected] wrote:


ello list,

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs