加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

activerecord – 表少模型的关联

发布时间:2020-12-17 02:05:51 所属栏目:百科 来源:网络整理
导读:我一直在尝试在FrontEnd上实现关联,但目前应用程序没有任何数据库直接与网站连接,因此我们无法使用ActiveRecord并且只使用ActiveModel来支持验证和核心功能一个模型.现在我们需要使用我们将要与对象一起发送的嵌套属性,与User关联的地址,因此我们需要首先在
我一直在尝试在FrontEnd上实现关联,但目前应用程序没有任何数据库直接与网站连接,因此我们无法使用ActiveRecord并且只使用ActiveModel来支持验证和核心功能一个模型.现在我们需要使用我们将要与对象一起发送的嵌套属性,与User关联的地址,因此我们需要首先在相应的模型上定义关联.但是在定义关联之后,它会在用户模型上抛出未定义方法“has_many”的异常.我正在寻找在我们的网站上实现它的方法,并实现嵌套属性的逻辑.
如果您可以向我推荐任何与此相关的内容,或者您??过去遇到过这样的问题,那将会很棒.
我也尝试过使用gem https://github.com/softace/activerecord-tableless但不适合我的方法.我还添加了一个tableless.rb

tableless.rb

class Tableless < ActiveRecord::Base     
  def self.column(name,sql_type = nil,default = nil,null = true)
    columns << ActiveRecord::ConnectionAdapters::Column.new( name.to_s,default,sql_type.to_s,null )
  end

  def self.columns()
    @columns ||= [];
  end

  def self.columns_hash
    h = {}
    for c in self.columns
    h[c.name] = c
    end
    return h
  end

  def self.column_defaults
    Hash[self.columns.map{ |col|
    [col.name,col.default]
    }]
  end
  def self.descends_from_active_record?
    return true
  end
  def persisted?
    return false
  end
# override the save method to prevent exceptions
end

但是获得以下异常异常:

控制台错误:

ActiveRecord::ConnectionNotEstablished: ActiveRecord::ConnectionNotEstablished
    from /home/cis/.rvm/gems/ruby-2.0.0-p0@website/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:546:in `retrieve_connection'
    from /home/cis/.rvm/gems/ruby-2.0.0-p0@website/gems/activerecord-4.0.0/lib/active_record/connection_handling.rb:79:in `retrieve_connection'
    from /home/cis/.rvm/gems/ruby-2.0.0-p0@website/gems/activerecord-4.0.0/lib/active_record/connection_handling.rb:53:in `connection'
    from /home/cis/.rvm/gems/ruby-2.0.0-p0@website/gems/activerecord-4.0.0/lib/active_record/model_schema.rb:203:in `table_exists?'
    from /home/cis/.rvm/gems/ruby-2.0.0-p0@website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/primary_key.rb:92:in `get_primary_key'
    from /home/cis/.rvm/gems/ruby-2.0.0-p0@website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/primary_key.rb:77:in `reset_primary_key'
    from /home/cis/.rvm/gems/ruby-2.0.0-p0@website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/primary_key.rb:65:in `primary_key'
    from /home/cis/.rvm/gems/ruby-2.0.0-p0@website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/primary_key.rb:79:in `reset_primary_key'
    from /home/cis/.rvm/gems/ruby-2.0.0-p0@website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/primary_key.rb:65:in `primary_key'
    from /home/cis/.rvm/gems/ruby-2.0.0-p0@website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/write.rb:32:in `write_attribute'
    from /home/cis/.rvm/gems/ruby-2.0.0-p0@website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/dirty.rb:70:in `write_attribute'
    from /home/cis/.rvm/gems/ruby-2.0.0-p0@website/gems/activerecord-4.0.0/lib/active_record/attribute_methods/write.rb:19:in `__temp__9646='
    from /home/cis/API_OTGJ/Tableless/app/models/book.rb:13:in `block in initialize'
    from /home/cis/API_OTGJ/Tableless/app/models/book.rb:12:in `each'
    from /home/cis/API_OTGJ/Tableless/app/models/book.rb:12:in `initialize'
    from /home/cis/.rvm/gems/ruby-2.0.0-p0@website/gems/activerecord-4.0.0/lib/active_record/inheritance.rb:27:in `new'
    from /home/cis/.rvm/gems/ruby-2.0.0-p0@website/gems/activerecord-4.0.0/lib/active_record/inheritance.rb:27:in `new'
    from (irb):19
    from /home/cis/.rvm/gems/ruby-2.0.0-p0@website/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
    from /home/cis/.rvm/gems/ruby-2.0.0-p0@website/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
    from /home/cis/.rvm/gems/ruby-2.0.0-p0@website/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

解决方法

你似乎忘了打电话给这个方法

has_no_table

在你的模型上,按照https://github.com/softace/activerecord-tableless#usage.在他们的例子中:

class ContactMessage < ActiveRecord::Base
  has_no_table
  column :name,:string
  column :email,:string
  validates_presence_of :name,:email
end

希望这可以帮助. =)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读