Jo Erlang
Jo Erlang

Reputation: 327

Creating several objects with relationships in rails

I'm new to rails.

I have a signup form, where the user can create a project, at the same time, as signing up. The project should get created, and the new user is made "Admin" of the project.

I have the following models:

class Project < A::B
  has_many :roles
  has_many :users, :through => :roles
  has_one :admin, :through => :roles, :conditions => "role.name = 'admin'"
  has_many :members, :through => :roles, :conditions => "role.name = 'member'"
end 


class User < A::B
  has_many :roles
  has_many :projects, :through => :roles
end

class Role < A::B
   belongs_to :projects
   belongs_to :users
end 

I'm looking for away to create encapsulate the setup of the relationship in the model layer, and make it easy to create forms, show errors etc in the view, for all objects in the relationship.

Hope I'm clear, newbie to rails . Thanks

Upvotes: 1

Views: 106

Answers (2)

Aditya Kapoor
Aditya Kapoor

Reputation: 1570

You can also have a look at the screencasts by Ryan Bates:

Upvotes: 2

nathanvda
nathanvda

Reputation: 50057

First off, to make creating the forms easier, I would recommend using a gem like formtastic or simple_form. Secondly, to make the nested model forms easier, I would recommend using a gem like cocoon, which works perfectly together with either formtastic or simple_form. Cocoon makes it easy to create dynamic nested model forms.

Hope this helps.

Upvotes: 1

Related Questions