SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
-
Feb 16, 2006, 00:08 #1
- Join Date
- Feb 2006
- Posts
- 3
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Code works in console, but not in controller!
I'm using Ruby on Rails.
My code works perfectly fine in the rails console (./script/console). But, when the same exact code is run in my controller, it spits out SQL errors. Here are the errors (log/development.log). The first highlighted line shows what the controller is spitting, while the second highlighted line shows the same code working perfectly fine.
Here's the code in the model:
Code:has_and_belongs_to_many :contacts, :class_name => 'User', :association_foreign_key => 'contact_id'
Code:@user = User.find(:first) @user.contacts << @user
Code:$ ./script/console
Code:>> u = User.create => #<User:0x22d66b4 @attributes={"id"=>1, "avatar_id"=>nil, "first_name"=>nil, "last_name"=>nil, "password"=>nil, "login"=>nil, "middle_name"=>nil}, @errors=#<ActiveRecord::Errors:0x22d6074 @errors={}, @base=#<User:0x22d66b4 ...>>, @new_record_before_save=false, @new_record=false>
Code:>> u.contacts_count => 0
Code:>> u.contacts << u => [#<User:0x22d66b4 @contacts=[...], @attributes={"id"=>1, "avatar_id"=>nil, "first_name"=>nil, "last_name"=>nil, "password"=>nil, "login"=>nil, "middle_name"=>nil}, @errors=#<ActiveRecord::Errors:0x22d6074 @errors={}, @base=#<User:0x22d66b4 ...>>, @new_record_before_save=false, @new_record=false>]
Code:>> u.contacts_count => 1
-
Feb 16, 2006, 01:53 #2
- Join Date
- Feb 2006
- Posts
- 3
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I fixed it...
Some words of advice: Don't use sqlite3. At least not with Ruby on Rails. It will only cause pain and suffering to anybody who tries to do anything worth doing with it. Use MySQL.
-
Feb 16, 2006, 07:41 #3
Is contacts_count a method you've created yourself? Is there any reason why you don't just do:
Code:u.contacts.size
-
Feb 27, 2006, 06:31 #4
- Join Date
- Feb 2006
- Posts
- 3
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
No, contacts_count is a method made available by activerecord.
-
Feb 27, 2006, 11:45 #5
Originally Posted by aneeley
Bookmarks