# Complicated Active Record CRUD

**URL:** <https://www.ruby-forum.com/t/complicated-active-record-crud/89507>\
**Category:** Rails\
**Created:** [March 20, 2007, 10:21pm UTC](https://www.ruby-forum.com/t/complicated-active-record-crud/89507 "2007-03-20T22:21:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![isaacrtree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/65b543/32.png) [@isaacrtree](https://www.ruby-forum.com/u/isaacrtree)\
**Post date:** [March 20, 2007, 10:21pm UTC](https://www.ruby-forum.com/t/complicated-active-record-crud/89507/1 "2007-03-20T22:21:20Z")

</div>

I’m having a lot of problems getting my head around how to do database  
editing with Rails, when I have the following tables and relationships  
(for brevity I’ve shortened the table listings - also IDs are added  
automatically, so I didn’t explicitly code them):

create\_table :projects do |t|  
t.column :name, :text  
end

create\_table :positions do |t|  
t.column :task\_id, :integer, :null =\> false  
t.column :user\_id, :integer, :default =\> 0  
end

create\_table :users\_projects, :id =\> false do |t|  
t.column :user\_id, :integer, :null =\> false  
t.column :project\_id, :integer, :null =\> false  
end

```
add_index :users_projects, [:user_id, :project_id]

```

create\_table :tasks do |t|  
t.column :project\_id, :integer, :null =\> false  
t.column :name, :text  
end

create\_table :users do |t|  
t.column :username, :text  
t.column :password, :text  
end

class User \< ActiveRecord::Base

has\_and\_belongs\_to\_many :positions  
has\_and\_belongs\_to\_many :tasks  
has\_and\_belongs\_to\_many :projects

end  
class Task \< ActiveRecord::Base

has\_and\_belongs\_to\_many :users  
has\_many :positions  
belongs\_to :project

end  
class Project \< ActiveRecord::Base

has\_and\_belongs\_to\_many :users  
has\_many :positions  
has\_many :tasks

end  
class Position \< ActiveRecord::Base

belongs\_to :task  
belongs\_to :project  
has\_and\_belongs\_to\_many :users

end

Now, I want to know how to add a project, using a user ID, and a  
position of ‘Project Manager’ - which is tied to the task ‘General’.

I got the form data, but I got lost when I cam to Project.new… Should  
I list every table (table.new) or preface them with  
Project.new(params[:task, :project…])??

Also, I would like to know what the correct ‘find’ command would be to  
find a project based on user ID, then list it with all its tasks and  
positions and users shown, as a kind of summary. Here I got as far as  
Project.find before all these doubts began assailing me. I didn’t want  
to do one table search, save the variable, and then do another, when  
Rails should allow me to search them all at once using the Models I laid  
out right?

Finally, I would like to know the correct delete method for removing a  
Project and all items tied to it, and how to delete a single position  
from a project’s task.

Also, I would like to know if I’ve possibly set things up wrongly with  
the database.

Help please! Thanks in advance 😃

---

<div class="post-metadata">

**Author:** ![isaacrtree](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/65b543/32.png) [@isaacrtree](https://www.ruby-forum.com/u/isaacrtree)\
**Post date:** [March 21, 2007, 2:44pm UTC](https://www.ruby-forum.com/t/complicated-active-record-crud/89507/2 "2007-03-21T14:44:08Z")

</div>

Anybody help with this? It’s really annoying…
