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

**URL:** <https://www.ruby-forum.com/t/newbie-question-where-do-i-update-the-crud-statements-if-they-exist/182361>\
**Category:** Rails\
**Created:** [January 27, 2010, 1:18am UTC](https://www.ruby-forum.com/t/newbie-question-where-do-i-update-the-crud-statements-if-they-exist/182361 "2010-01-27T01:18:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Souschef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3d9bf3/32.png) [@Souschef](https://www.ruby-forum.com/u/Souschef)\
**Post date:** [January 27, 2010, 1:18am UTC](https://www.ruby-forum.com/t/newbie-question-where-do-i-update-the-crud-statements-if-they-exist/182361/1 "2010-01-27T01:18:07Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Souschef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3d9bf3/32.png) [@Souschef](https://www.ruby-forum.com/u/Souschef)\
**Post date:** [January 27, 2010, 1:59am UTC](https://www.ruby-forum.com/t/newbie-question-where-do-i-update-the-crud-statements-if-they-exist/182361/2 "2010-01-27T01:59:51Z")

</div>

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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Souschef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3d9bf3/32.png) [@Souschef](https://www.ruby-forum.com/u/Souschef)\
**Post date:** [January 27, 2010, 6:03am UTC](https://www.ruby-forum.com/t/newbie-question-where-do-i-update-the-crud-statements-if-they-exist/182361/3 "2010-01-27T06:03:00Z")

</div>

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.

> **[GitHub - railslist/craigslist-clone: railslist is a craigslist clone](https://github.com/railslist/craigslist-clone)**
>
> railslist is a craigslist clone. Contribute to railslist/craigslist-clone development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![Souschef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3d9bf3/32.png) [@Souschef](https://www.ruby-forum.com/u/Souschef)\
**Post date:** [January 27, 2010, 7:50pm UTC](https://www.ruby-forum.com/t/newbie-question-where-do-i-update-the-crud-statements-if-they-exist/182361/4 "2010-01-27T19:50:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Souschef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3d9bf3/32.png) [@Souschef](https://www.ruby-forum.com/u/Souschef)\
**Post date:** [January 27, 2010, 2:00pm UTC](https://www.ruby-forum.com/t/newbie-question-where-do-i-update-the-crud-statements-if-they-exist/182361/5 "2010-01-27T14:00:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Souschef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3d9bf3/32.png) [@Souschef](https://www.ruby-forum.com/u/Souschef)\
**Post date:** [January 30, 2010, 4:41pm UTC](https://www.ruby-forum.com/t/newbie-question-where-do-i-update-the-crud-statements-if-they-exist/182361/6 "2010-01-30T16:41:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Souschef](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3d9bf3/32.png) [@Souschef](https://www.ruby-forum.com/u/Souschef)\
**Post date:** [January 30, 2010, 1:44pm UTC](https://www.ruby-forum.com/t/newbie-question-where-do-i-update-the-crud-statements-if-they-exist/182361/7 "2010-01-30T13:44:36Z")

</div>

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](http://github.com/binarylogic/authlogic_example)

# 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
