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