从控制台获取 Rails 关联

发布于 2024-11-04 21:04:26 字数 71 浏览 4 评论 0原文

我的 Rails 应用程序中有一个模型 - User。我希望在 Rails 控制台中列出所有关联以及关联类型(1-1、1-多)。

I have a model in my Rails application - User. I want all the associations to be listed in rails console, along with the type of association(1-1, 1-many).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

若相惜即相离 2024-11-11 21:04:26
User.reflect_on_all_associations

这将返回类似于以下的关联数组:

#<ActiveRecord::Reflection::AssociationReflection:0x00000105575548 @macro=:has_many, @name=:posts, @options={}, @active_record=User(id: integer, login: string), @collection=false>

示例代码:

reflections = User.reflect_on_all_associations
reflections.each do |reflection|
  puts ":#{reflection.macro} => :#{reflection.name}"
end
User.reflect_on_all_associations

This will return an array of associations similar to this:

#<ActiveRecord::Reflection::AssociationReflection:0x00000105575548 @macro=:has_many, @name=:posts, @options={}, @active_record=User(id: integer, login: string), @collection=false>

Sample code:

reflections = User.reflect_on_all_associations
reflections.each do |reflection|
  puts ":#{reflection.macro} => :#{reflection.name}"
end
微暖i 2024-11-11 21:04:26

使用 gem pry-rails 您将能够访问模型、其列和关系。将其包含在您的 Gemfile 中,然后运行 ​​bundle。当您在 pry 控制台中时,可以使用命令 show-models。您将获得有关所有型号的信息。

您还可以运行 show-model (Model_Name) 来获取有关特定模型的信息

Using the gem pry-rails you will be able to access the model, its columns and relationships. Include it in your Gemfile, then you run bundle. You can use the command show-models when you are in your pry console. and you will get the information about all your models.

You can also run show-model (Model_Name) to get information about a specific model

秋凉 2024-11-11 21:04:26

将其添加到 /lib 下的某个位置。例如clone_deep.rb。

module CloneDeep
  def clone_deep
    kopy = clone
    self.class.reflect_on_all_associations.each do |association|
      next if association.macro == :belongs_to
      cloned_object = case association.macro
                        when :has_many
                          self.send(association.name).collect { |item| item.clone_deep }
                        when :has_one
                          self.send(association.name) && self.send(association.name).clone_deep
                        else
                          clone
                      end
      kopy.send("#{association.name}=", cloned_object)
    end
    return kopy
  end
end

在 config/initializers/ 文件夹下创建新的初始化程序。在此文件中粘贴

ActiveRecord::Base.send(:include, CloneDeep)

现在您可以克隆模型及其所有 has_one 和 hos_many 关联。

cloned_person = person.clone_deep
cloned_person.save

Add this some where is under /lib. For example clone_deep.rb.

module CloneDeep
  def clone_deep
    kopy = clone
    self.class.reflect_on_all_associations.each do |association|
      next if association.macro == :belongs_to
      cloned_object = case association.macro
                        when :has_many
                          self.send(association.name).collect { |item| item.clone_deep }
                        when :has_one
                          self.send(association.name) && self.send(association.name).clone_deep
                        else
                          clone
                      end
      kopy.send("#{association.name}=", cloned_object)
    end
    return kopy
  end
end

Create new initializer under config/initializers/ folder. Inside this file paste

ActiveRecord::Base.send(:include, CloneDeep)

Now you be able to clone model with all it's has_one and hos_many associations.

cloned_person = person.clone_deep
cloned_person.save
顾忌 2024-11-11 21:04:26

因为我是新用户,所以我无法澄清/回复其他人的帖子。我会注意到,在检查关联中的任何更改之前,您需要重新加载 Rails 控制台。

Because I'm a new user, I am unable to clarify/reply to other's posts. I'll note that you need to reload the rails console before checking any changes in associations.

朕就是辣么酷 2024-11-11 21:04:26

您可以针对任何特定的情况执行此操作:-
用户 = User.reflect_on_association(:user_profile)

用户宏

You can do this for any particular :-
user = User.reflect_on_association(:user_profile)
and
user.macro

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文