简体   繁体   English

在rails中的has_many中的关联

[英]Associations in has_many , belong_to in rails

I am stuck at a small problem,but it has been so long for me trying to figure out what am i doing wrong. 我陷入了一个小问题,但是我试图弄清楚我在做什么错了已经很长时间了。 My scenario is i have a existing model user and now i create another model called `user_comment'. 我的情况是我有一个现有的模型user ,现在我创建另一个名为“ user_comment”的模型。 I have created below mentioned details: 我创建了以下提到的详细信息:

User model: 用户模型:

class User < ActiveRecord::Base
has_many :user_comments
end

User_comment Model: 用户评论模型:

 class UserComment < ActiveRecord::Base
    belongs_to :user
    end

Migration File: 迁移文件:

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

After running rake db:migrate i went to rails console and then to setup the relation between two tables i did the following and nothing is working 运行rake db:migrate我进入了rails console ,然后设置了两个表之间的关系,然后执行了以下操作,但没有任何反应

obj1= User.first

I added first new row in user_comments table and then did .. 我在user_comments表中添加了第一行,然后执行了..

obj2= UserComment.first

Doing obj1.obj2= obj2 is giving me 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>'

Please help me how to form an association .. 请帮助我如何建立协会..

obj1.obj2= obj2 is wrong, you need a space between obj1.obj2 and = . obj1.obj2= obj2错误,您需要在obj1.obj2=之间obj1.obj2一个空格。

But ob1.obj2 make no sense too(there is no obj2 method in User ). 但是ob1.obj2也没有意义( User没有obj2方法)。

Add an object to an association you could do: 将对象添加到关联中,您可以执行以下操作:

user = User.first
comment = UserComment.first

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

Where does SmileComment suddenly come from? SmileComment突然来自哪里?

Anyway, there are several things going wrong. 无论如何,有些事情出了问题。 First off, I would change the name of your model from UserComment to Comment. 首先,我将您的模型名称从UserComment更改为Comment。 The fact that a comment belongs to an user is already made clear through your association. 注释属于用户这一事实已通过您的关联得到了明确说明。 Calling User.first.user_comments seems a bit akward. 调用User.first.user_comments似乎有点笨拙。

Let us start with a really basic example: 让我们从一个非常基本的例子开始:

class User < ActiveRecord::Base
  has_many :comments
end

class Comment < ActiveRecord::Base
  belongs_to :user
end

As you did in your migration, the comments needs a user_id to reference the user it belongs to. 正如您在迁移中所做的那样,注释需要一个user_id来引用其所属的用户。 After running the migration, calling the association is dead simple: 运行迁移后,调用关联非常简单:

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

Or: 要么:

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

obj1.obj2= obj2 => self-referential loop? obj1.obj2= obj2 =>自引用循环?


You'll be best reading the Rails ActiveRecord associations guide - you'll find what you're dealing with is actually relatively simple to remedy 您最好阅读Rails ActiveRecord关联指南-您会发现实际上所要解决的问题相对简单

As described by @rails4guides.com , you'll be best renaming your UserComment model to just Comment (as this will allow you to associate any data you need with it -- if you wanted to extend later): @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

This is actually down to relational database naming conventions , whereby each table's data can be associated with another's. 这实际上取决于关系数据库的命名约定 ,据此,每个表的数据都可以与另一个表相关联。 They do this with foreign_keys - which is how Rails uses the associations defined in your models to create the likes of @user.comments : 他们使用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

So if you want to give the user a set of comments, you'll just need to call the User object from your model, and since Rails' ActiveRecord associations automatically call any relational data through your schemas, you'll get comments appended to the @user object with @user.comments 因此,如果要向用户提供一组注释,则只需要从模型中调用User对象,并且由于Rails的ActiveRecord关联会通过您的架构自动调用任何关系数据,因此您可以将comments附加到@user对象与@user.comments

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails has_many / belong_to协会 - Rails has_many/belong_to Associations 如何使用联接表在Rails中创建has_many和belong_to一个关联? - How to create has_many and belong_to one associations in Rails using a join table? Rails belongs_to has_many具有自定义外键 - Rails belong_to has_many with a custom foreign key Rails为多个主键在belong_to和has_many之间设置了基于角色的关系 - Rails set role based relationship between belong_to and has_many for multiple primary keys 更新has_many / belong_to关系中大量关联对象的行(Rails 4,PostgreSQL 9.4,activeadmin) - Update massive number of rows of associated objects in has_many/belong_to relations (Rails 4, postgresql 9.4, activeadmin) 关于belong_to,has_one,has_many的困惑 - Confusion regarding belong_to, has_one, has_many Rails:如果Foo has_many:吧,那么所有:吧需要属于一个Foo吗? - Rails: If Foo has_many :bars, do all :bars need to belong_to a Foo? 通过双归属表设置has_many - Setting has_many through double belong_to table has_many,belong_to和两者之间的区别 - Difference between has_many, belong_to, and both 在Ruby on Rails中,模型“ has_many”和“ belong_to”如何使用除主ID之外的其他字段? - In Ruby on Rails, how can a model “has_many” and “belong_to” using a different field other than primary ID?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM