Association for a table smaller model

I am trying to implement an association on FrontEnd, but the application does not currently have a database directly linked to the website, so we cannot use ActiveRecord and use ActiveModel to support validations and the main characteristics of the model. Now, when we need to use the nested attributes that we are going to send along with the object, the addresses are associated with the user, so for this we need to first determine the association on the corresponding model. But after defining the association, he throws an undefined "has_many" method exception in the user model. I'm currently looking for a way to implement it on our website and implement the logic of nested attributes. It would be great if you offered me something related to this, or if you have encountered such a problem in the past.
I also tried using this method using gem https://github.com/softace/activerecord-tableless , but not working for me. I also added 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

But I get the following exception. An exception:

Console Error:

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>'
+4
source share
1 answer

It seems that you forgot to call the method

has_no_table

On your model, according to https://github.com/softace/activerecord-tableless#usage . In their example:

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

Hope this helps. =)

+1
source

Source: https://habr.com/ru/post/1524057/


All Articles