# Problem with Insertion

**URL:** <https://www.ruby-forum.com/t/problem-with-insertion/165253>\
**Category:** Rails\
**Created:** [April 10, 2009, 8:40pm UTC](https://www.ruby-forum.com/t/problem-with-insertion/165253 "2009-04-10T20:40:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![avalin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b3f665/32.png) [@avalin](https://www.ruby-forum.com/u/avalin)\
**Post date:** [April 10, 2009, 8:40pm UTC](https://www.ruby-forum.com/t/problem-with-insertion/165253/1 "2009-04-10T20:40:22Z")

</div>

I have a model called Room, and I am trying to allow the user to create  
favorites to bookmark certain rooms. The Favorites table contains the  
fields “room\_id” and “user\_id”, and in the view for /rooms/:id I have  
the following code:

`<%=link_to("Add to Favorites", favorites_url(:favorite =>
{:user_id => @user.id, :room_id => @room.id}), :method => :post)
%>`  
In favorites\_controller, my create method looks like this:

[code=]def create  
@room = Room.find(:first, :conditions =\> [‘id = ?’,  
params[:favorite][:room\_id]])  
@favorite = Favorite.new(params[:favorite])

```
respond_to do |format|

  puts @favorite.room_id
  puts @favorite.user_id

  if @favorite.save
    flash[:notice] = 'Room was successfully added to your

```

favorites!’  
format.html { redirect\_to(rooms\_path(@room)) }  
format.xml { render :xml =\> @favorite, :status =\> :created,  
:location =\> @favorite }  
else  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @favorite.errors, :status =\>  
:unprocessable\_entity }  
end  
end  
end[/code]  
The two puts statements print out the room ID and user ID correctly, but  
in the site itself I get the following error:

SQLite3::SQLException: favorites.user\_id may not be NULL: INSERT INTO  
“favorites” (“updated\_at”, “user\_id”, “room\_id”, “created\_at”)  
VALUES(‘2009-04-10 18:04:46’, NULL, NULL, ‘2009-04-10 18:04:46’)

If I manually go into the GUI for SQLite3, I can enter in a new row for  
favorites fine, but when I try creating a new Favorite through the  
console, I receive an error. My model code looks like this:

[code=]class Favorite \< ActiveRecord::Base  
belongs\_to :room  
belongs\_to :user

named\_scope :for\_user, lambda { |user\_id| { :conditions =\> [“user\_id =  
?”, user\_id] }}

attr\_accessor :room\_id, :user\_id  
validates\_presence\_of :room\_id, :user\_id =\> “must be filled in”

end[/code]  
Any thoughts? I don’t believe it is a problem with the controller since  
it is printing the correct values, but I can’t figure out what is wrong  
with the model or database.

---

<div class="post-metadata">

**Author:** ![avalin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b3f665/32.png) [@avalin](https://www.ruby-forum.com/u/avalin)\
**Post date:** [April 10, 2009, 8:56pm UTC](https://www.ruby-forum.com/t/problem-with-insertion/165253/2 "2009-04-10T20:56:18Z")

</div>

On 10 Apr 2009, at 19:40, Avalin Swift wrote:

> attr\_accessor :room\_id, :user\_id

Don’t do this: you are overwriting the accessors that Active Record  
provides with ones that will stick the values in places Active Record  
won’t find.

Fred
