【问题标题】:Associations in has_many , belong_to in railshas_many 中的关联,rails 中的 belongs_to
【发布时间】:2014-03-31 08:33:49
【问题描述】:

我遇到了一个小问题,但我花了很长时间才试图弄清楚我做错了什么。我的场景是我有一个现有的模型user,现在我创建了另一个名为“user_comment”的模型。我创建了以下提到的详细信息:

用户模型:

class User < ActiveRecord::Base
has_many :user_comments
end

用户评论模型:

 class UserComment < ActiveRecord::Base
    belongs_to :user
    end

迁移文件:

class CreateUserComments < ActiveRecord::Migration
  def change
    create_table :user_comments do |t|
      t.integer :user_id
      t.string  :comments
      t.timestamps
    end
  end
end

运行rake db:migrate 后,我转到rails console,然后设置两个表之间的关系,我执行了以下操作,但没有任何效果

obj1= User.first

我在 user_cmets 表中添加了第一个新行,然后做了..

obj2= UserComment.first

obj1.obj2= obj2是给我的

NoMethodError: undefined method `obj2=' for #<User:0x00000005f8e850>
    from /home/insane/.rvm/gems/ruby-2.1.0/gems/activemodel-3.2.11/lib/active_model/attribute_methods.rb:407:in `method_missing'
    from /home/insane/.rvm/gems/ruby-2.1.0/gems/activerecord-3.2.11/lib/active_record/attribute_methods.rb:149:in `method_missing'
    from (irb):3
    from /home/insane/.rvm/gems/ruby-2.1.0/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start'
    from /home/insane/.rvm/gems/ruby-2.1.0/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in `start'
    from /home/insane/.rvm/gems/ruby-2.1.0/gems/railties-3.2.11/lib/rails/commands.rb:41:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

请帮我如何组建一个协会..

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 rails-activerecord model-associations


    【解决方案1】:

    obj1.obj2= obj2 错误,obj1.obj2= 之间需要一个空格。

    但是ob1.obj2 也没有意义(User 中没有 obj2 方法)。

    将对象添加到您可以执行的关联中:

    user = User.first
    comment = UserComment.first
    
    # if both object are not nil, then you could do below
    user.user_commentes << comment
    

    【讨论】:

      【解决方案2】:

      SmileComment 突然从何而来?

      无论如何,有几件事出了问题。首先,我会将您的模型名称从 UserComment 更改为 Comment。评论属于用户这一事实已通过您的关联明确。调用 User.first.user_cmets 似乎有点尴尬。

      让我们从一个非常基本的例子开始:

      class User < ActiveRecord::Base
        has_many :comments
      end
      
      class Comment < ActiveRecord::Base
        belongs_to :user
      end
      

      正如您在迁移中所做的那样,cmets 需要一个 user_id 来引用它所属的用户。运行迁移后,调用关联非常简单:

      User.first.comments # Gives all comments belonging to that user
      

      或者:

      Comment.first.user # Gives the user that belongs to that comment
      

      【讨论】:

        【解决方案3】:

        obj1.obj2= obj2 => 自引用循环?


        您最好阅读 Rails ActiveRecord associations 指南 - 您会发现您正在处理的问题实际上比较容易解决

        @rails4guides.com 所述,您最好将您的UserComment 模型重命名为Comment(因为这将允许您将所需的任何数据与其关联——如果您想稍后扩展):

        #app/models/user.rb
        Class User < ActiveRecord::Base
            has_many :comments
        end
        
        #app/models/comment.rb
        Class Comment < ActiveRecord::Base
            belongs_to :user
        end
        

        这实际上是relational database naming conventions,每个表的数据可以与另一个表的数据相关联。他们使用foreign_keys 来做到这一点——这就是Rails 使用模型中定义的关联来创建@user.comments 之类的方式:

        class Comments < ActiveRecord::Migration
          def change
            create_table :comments do |t|
              t.integer :user_id => this is the foreign_key, which means Rails can append these objects to your `user` object
              t.string  :comments
              t.timestamps
            end
          end
        end
        

        因此,如果您想为用户提供一组 cmets,您只需从模型中调用 User 对象,并且由于 Rails 的 ActiveRecord 关联自动通过您的模式调用任何关系数据,您将使用@user.commentscomments 附加到@user 对象

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-04-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-09
          • 2015-09-11
          • 2011-04-21
          • 1970-01-01
          相关资源
          最近更新 更多