Newbie question: Where do I update the CRUD statements (if they exist)?

I’ve added a foreign key to a table (specifically, user_Id to my
projects table so I can select ‘myprojects’) and now when I add a
project, the user_id is not being saved in the projects table. In a
former life, I would update my SQL statements, but this is rails.
Where do I update my CRUD statements?

Souschef wrote:

I’ve added a foreign key to a table (specifically, user_Id to my
projects table so I can select ‘myprojects’) and now when I add a
project, the user_id is not being saved in the projects table.

What errors are you getting?

In a
former life, I would update my SQL statements, but this is rails.
Where do I update my CRUD statements?

Wherever they are. What ActiveRecord call is causing problems? Find it
and fix it.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Souschef wrote:

I’ve added a foreign key to a table (specifically, user_Id to my
projects table so I can select ‘myprojects’) and now when I add a
project, the user_id is not being saved in the projects table. In a
former life, I would update my SQL statements, but this is rails.
Where do I update my CRUD statements?

download my craigslist clone software (developed in ror), go through
them and learn. its real fun.

I believe you will have to learn about associations (or relationships
between tables, as another post has already noted). Look into
‘belongs_to’ and ‘has_*’ relationships (probably ‘has_one’ or
‘has_many’). That will most likely teach you what you need.

Can you show us the code that is not working?

Make sure you have the relationship specified in your model so Rails
knows it’s there. so you should have

class Project < ActiveRecord::Base
belongs_to :user
end

so that you can do:

Project.new :user => @user
or
Project.new :user_id => 12
and then after project.save:
project.user #=> #User:...
project.user_id #=> 12

When you create a project you can do:

@project = Project.find(params[:id])
@task = Task.create(params[:task])
@project.tasks << @task

or something similar. this will automatically out the user id I the
project table for you.

Here are my models. I think the issue is how I have the user
associated with the project. I recently integrated the authlogic
plugin and associated the user to the project table (see below). I’m
stumped because when I associated ‘tasks’ to ‘projects’, the foreign
key for project was inserted into tasks. But for some reason now that
I have ‘users’ associated with projects, the user_id is not inserted
into the project table on a save when a user is logged in. Should I
be associating ‘projects’ to the ‘UserSession’ instead (model also
included below, but currently empty)? Or do I need to declare some
sort of logic that saves user_id when a user is actually logged in?
Thanks in advance for input!

--------------------Project model--------------------
class Project < ActiveRecord::Base
validates_presence_of :name

allow ordering of tasks by step_number

has_many :tasks, :dependent => :destroy, :order => ‘step_number ASC’
accepts_nested_attributes_for :tasks, :reject_if => lambda { |a|
a.values.all?(&:blank?) }, :allow_destroy => true

def task_attributes=(task_attributes)
task_attributes.each do |attributes|
tasks.build(attributes)
end
end

Following statements tie Projects to users

belongs_to :user

end

--------------------User model--------------------
class User < ActiveRecord::Base

following line commented out. Came from authlogic, but not sure

what it means…

attr_accessible :username, :email, :password

Added following line from railscast demo. Note:

GitHub - binarylogic/authlogic_example: An example rails app using the Authlogic authentication library

has an optional block for passing other config options, but didn’t

go there for now…

acts_as_authentic

has_many :projects
end

------------------User Session model---------------
class UserSession < Authlogic::Session::Base
end