Forum: Ruby on Rails Relationship issue with activerecord rails

43de7db6835d655afccb62eef43dbf89?d=identicon&s=25 Steve Knit (pringleso)
on 2012-05-29 13:56
I am still learning Rails, I've read some books and did some hands on at
codeschool.com and now I'm trying to write my first simple app from
scratch.

I'm using devise for authentication, but since i still kinda suck at
rails, I haven't gotten email confirmation working so currently, for
testing purposes only Admin users can take actions.

Here are my models:


    [loluser@fedora models]$ ls
    admin.rb  pet.rb  user.rb
    [loluser@fedora models]$ cat admin.rb
    class Admin < ActiveRecord::Base
      has_many  :pets
      devise :database_authenticatable, :registerable, :timeoutable,
:validatable,
             :timeout_in => 20.minutes
    end
    [loluser@fedora models]$ cat pet.rb
    class pet < ActiveRecord::Base
      belongs_to :admin
    end
[loluser@fedora models]$


In my controller, I want to display Admin[1]'s pets in the index  so i
have this code:

      class petsController < ApplicationController
      before_filter :authenticate_admin!
      # GET /pets
      # GET /pets.xml
      def index
      admin=Admin.find(1)
        @pets = pet.admin.all

        respond_to do |format|
          format.html # index.html.erb
          format.xml  { render :xml => @pets }
        end
      end

However, I am getting this error:

    NoMethodError in petsController#index

    undefined method `admin' for #<Class:0x7f2daa7b0258>

    Rails.root: /home/loluser/dev/app2/devise_example
    Application Trace | Framework Trace | Full Trace

    app/controllers/pets_controller.rb:7:in `index'

Help will be appreciated and please let me know if I need to clarify
something.

Thanks in advance.
955103d80e386882143a78875fb727bf?d=identicon&s=25 Jeremy W. (jeremy_w)
on 2012-05-29 14:05
(Received via mailing list)
On 29 May 2012 12:56, Steve Knit <lists@ruby-forum.com> wrote:

>
>    class pet < ActiveRecord::Base
>      # GET /pets
>      # GET /pets.xml
>      def index
>      admin=Admin.find(1)
>        @pets = pet.admin.all
>

I'm guessing your pet object doesn't have an admin method?

Should "pet.admin.all" maybe be "admin.pets" ?
43de7db6835d655afccb62eef43dbf89?d=identicon&s=25 Steve Knit (pringleso)
on 2012-05-29 14:14
Thanks for pointing me to @pets = admin.pets.all but now I'm getting the
error

    ActiveRecord::StatementInvalid in PetsController#index

    SQLite3::SQLException: no such column: pets.admin_id: SELECT
"pets".* FROM "pets" WHERE ("pets".admin_id = 1)

    Rails.root: /home/loluser/dev/app2/devise_example
    Application Trace | Framework Trace | Full Trace

    app/controllers/pets_controller.rb:7:in `index'


I have already done the command "bundle exec rake:db migrate" so I'd
think that admin_id would already part of pets as you can see my models
in my original post:

    [loluser@fedora models]$ cat admin.rb
    class Admin < ActiveRecord::Base
      has_many  :pets
      devise :database_authenticatable, :registerable, :timeoutable,
:validatable,
             :timeout_in => 20.minutes
    end
    [loluser@fedora models]$ cat pet.rb
    class pet < ActiveRecord::Base
      belongs_to :admin
    end
955103d80e386882143a78875fb727bf?d=identicon&s=25 Jeremy W. (jeremy_w)
on 2012-05-29 14:20
(Received via mailing list)
On 29 May 2012 13:14, Steve Knit <lists@ruby-forum.com> wrote:

>
>    app/controllers/pets_controller.rb:7:in `index'
>

 The error message says that the pets table does not have a column
"admin_id". Have you checked to see whether your pets table has a column
called admin_id? What does your migration for the pets table look like?
43de7db6835d655afccb62eef43dbf89?d=identicon&s=25 Steve Knit (pringleso)
on 2012-05-29 14:26
Hello Jeremy, thanks for providing me your experience and quick
response! I'm not quite sure what tool to use to browse database, i did
a lot of PhP and I used to use PhPMyAdmin, but since i'm new to
rails/ruby i'm working sorta in the dark.

Here is my migration, sorry for being such a newb....


[loluser@fedora migrate]$ cat 20120527225053_create_pets.rb
class CreatePets < ActiveRecord::Migration
  belongs_to  :admin
  def self.up
    create_table :pets do |t|
      t.string :location
      t.string :public_key

      t.timestamps
    end
  end

  def self.down
    drop_table :pets
  end
end
955103d80e386882143a78875fb727bf?d=identicon&s=25 Jeremy W. (jeremy_w)
on 2012-05-29 15:02
(Received via mailing list)
On 29 May 2012 13:26, Steve Knit <lists@ruby-forum.com> wrote:

> Hello Jeremy, thanks for providing me your experience and quick
> response!
>

You're welcome! So you know, it's customary to post your responses in
the
relevant place of a thread, rather than top-posting. It makes the
conversation easier to follow.


>      t.string :public_key
>
>      t.timestamps
>    end
>  end
>
>  def self.down
>    drop_table :pets
>  end
> end
>

You have the "belongs_to :admin" method in the wrong place. I'm
interested
that this even runs!

Your create_table block should look like this:

   create_table :pets do |t|
     t.belongs_to  :admin
     t.string :location
     t.string :public_key

     t.timestamps
   end

In this context, the belongs_to is part of the database creation schema
and
so must be placed within the create_table call.

Try changing that and then redoing the migration (rake db:migrate:redo)
and
see whether that fixes things.
43de7db6835d655afccb62eef43dbf89?d=identicon&s=25 Steve Knit (pringleso)
on 2012-05-29 15:57
(Received via mailing list)
Thanks Jeremy, your code got me rolling. I will be doing more reading
http://guides.rubyonrails.org/association_basics.html
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.