Rails has many and belongs to one?

Add a creator_id column to your projects table for the creator relationship, and then add the associations to the models: class User :assigned_projects has_many :created_projects, :class_name => "Project", :foreign_key => :creator_id end class Project :assigned_projects belongs_to :creator, :class_name => "User", :foreign_key => :creator_id end api.rubyonrails.org/classes/ActiveRecord....

Thanks for your answer, this wasn't my issue though. It was: What I'm struggling to get my head around is the best way to assign the project creator (which by the way doesn't need to be user, it could be creator or something else descriptive, as long as it is of type User). – injekt Jun 30 at 14:29 @Nuby sure about what?

Sure that I'm struggling to get my head around assigning the user/creator to the project in an automated fashion? Yes – injekt Jun 30 at 14:47 Unless I misread @Pavling's answer (and your question), what he's proposing is that you create a second relationship with the User table, called 'Creator'. You could then call Project.

Creator to get the associated user from the CreatedProjects association. That would solve (I think) your problem. As far as how to assign it, you could do a before_create hook in the model to assign this to the currently logged in user, or some other user (depending on your project's logic).

– Nuby Jun 30 at 15:06 @injekt - yes, sorry, this code sets up the relationships - how you assign them is up to your code's operation. Personally, I'd have a line in the ProjectsController create action that sets @project. Creator = current_user (or whatever helper method you have for getting the logged-in user) – Pavling Jun 30 at 15:21 Well, that's embarrassing.

Setting the creator is fine if you think just doing that in the create action of my controller is ok, I think I was looking for unnecessary magic Rails usually provides. One thing I did have wrong though was I had has_many :created_projects, :class_name => "Project", :foreign_key => :creator_id but in project, I had belongs_to :creator, :class_name => 'User' without the foreign key, I guess I assumed it would work this out from the :creator reference. Thanks a lot guys!

– injekt Jun 30 at 17:40.

A has_and_belongs_to_many association creates a direct many-to-many connection with another model, with no intervening model. If you want to set up a one-to-one relationship between two models, you’ll need to add belongs_to to one, and has_one to the other. How do you know which is which?

The distinction is in where you place the foreign key (it goes on the table for the class declaring the belongs_to association), but you should give some thought to the actual meaning of the data as well. The has_one relationship says that one of something is yours – that is, that something points back to you. For example, it makes more sense to say that a supplier owns an account than that an account owns a supplier.

Rails offers two different ways to declare a many-to-many relationship between models. The second way to declare a many-to-many relationship is to use has_many :through. The simplest rule of thumb is that you should set up a has_many :through relationship if you need to work with the relationship model as an independent entity.

If you don’t need to do anything with the relationship model, it may be simpler to set up a has_and_belongs_to_many relationship (though you’ll need to remember to create the joining table in the database). You should use has_many :through if you need validations, callbacks, or extra attributes on the join model. A slightly more advanced twist on associations is the polymorphic association.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions