SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Member
    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'
    Here's the code in the controller:

    Code:
    @user = User.find(:first)
    @user.contacts << @user
    And here's some proof that it SHOULD work:

    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
    Somebody please help me. Otherwise, I think I just might have a stroke.

  2. #2
    SitePoint Member
    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.

  3. #3
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is contacts_count a method you've created yourself? Is there any reason why you don't just do:

    Code:
    u.contacts.size

  4. #4
    SitePoint Member
    Join Date
    Feb 2006
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, contacts_count is a method made available by activerecord.

  5. #5
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by aneeley
    No, contacts_count is a method made available by activerecord.
    Interesting. I wonder what the difference is. Perhaps that just runs a SELECT COUNT(*)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •