Rails 4:尝试访问关系时堆栈级别太深

时间:2014-05-30 15:01:38

标签: ruby-on-rails ruby ruby-on-rails-4

这是一次迁移

class AddAssociationsTable < ActiveRecord::Migration
    def change
        create_table :associations do |t|

            # user_id : Reference to the user profile to whom the association belongs
            t.belongs_to :user, null: false
            # secret_token : Secret Token used to make API request on behalf of the user
            t.string :secret_token, null: false
            # access_token : Access Token used to make API request on behalf of the user
            t.string :access_token, null: false
            # public : Allows users to make their information public or keep it just to themselves
            t.boolean :public, default: true

            t.timestamps
        end

        add_index :associations, :user_id
    end
end

这是第二次迁移

class AddUsersTable < ActiveRecord::Migration
    def change
        create_table :users do |t|

            # uid ; Twitter user ID
            t.integer :twitter_id

            # name : Real name provided to Twitter
            t.string :name

            # screen_name : Username on Twitter
            t.string :screen_name

            # description : Self Description on Twitter
            t.text :description

            # location : Locatin provided by User to Twitter
            t.string :location

            # lang : Language used on Twitter
            t.string :lang

            # profile_img : URL to profile image
            t.string :profile_image_url

            # tweet_count : Amount of tweets posted
            t.integer :statuses_count

            # followers_count : Amount of follwers at the current moment
            t.integer :followers_count

            # friends_count : Amount of friends/following at the current moment
            t.integer :friends_count

            # listed_count : Amount of lists in which the user is a part of
            t.integer :listed_count

            # verified : Verified by Twitter to be the claimed person (Used mostly for celebraties)
            t.boolean :verified

            # created_at : date at which user joined Twitter
            t.date :created_at
        end

        add_index :users, :twitter_id
        add_index :users, :screen_name
    end
end

然后我有以下模型

class Association < ActiveRecord::Base
    devise :omniauthable, :omniauth_providers => [:twitter]

    belongs_to :user
end

class User < ActiveRecord::Base
    has_one :association
end

每当我运行这样的东西时:     User.first.association

我的堆栈级别太深了。有谁知道问题在哪里?

完整追踪:

actionpack (4.0.4) lib/action_dispatch/middleware/reloader.rb:70

2 个答案:

答案 0 :(得分:1)

问题是由名称冲突引起的。当您声明has_one :association时,rails会查找常量Association。你会期望它会指向你模型Association。不幸的是,在User类中已经存在一个名为Association的常量,它被定义为ActiveRecord::Associations::Association

简而言之,您需要重命名模型 - 它不能命名为“关联”

答案 1 :(得分:0)

我认为重命名关联应该可以解决问题吗?

has_one :new_name, class_name: "association"