activerecord 3.2.2 association issues with multiple connections - mysql

can somebody point out some problems that might occur when im having association problem like this?
my classes are:
class BaseDB < ActiveRecord::Base
self.abstract_class = true
def self.load_configs
self.configurations = YAML::load(load_file)
end
def self.load_file
File.open(file_path)
end
def self.file_path
File.join(File.expand_path('../../../',__FILE__),"config/database.yml")
end
end
class User < BaseDB
has_many :things
end
class Thing < BaseDB
belongs_to :user
end
when I try to run in the console
>BaseDB.load_configs
=> {"my_connection"=>{"database"=>"db2", "adapter"=>"mysql2", "password"=>secret, "host"=>"localhost", "user"=>"root"}, "my_other_connection_test"=>{"database"=>"db1", "adapter"=>"mysql2", "password"=>secret, "host"=>"localhost", "user"=>"root"}}
> BaseDB.establish_connection :my_connection
=> #<ActiveRecord::ConnectionAdapters::ConnectionPool:0x7f69c7a38b98 #mon_entering_queue=[], #automatic_reconnect=true, #mon_count=0, #queue=#<MonitorMixin::ConditionVariable:0x7f69c7a38a58 #waiters=[], #monitor=#<ActiveRecord::ConnectionAdapters::ConnectionPool:0x7f69c7a38b98 ...>>, #connections=[], #mon_owner=nil, #reserved_connections={}, #spec=#<ActiveRecord::Base::ConnectionSpecification:0x7f69c7a49718 #adapter_method="mysql2_connection", #config={:password=>"####", :adapter=>"mysql2", :user=>"root", :host=>"localhost", :database=>"db2"}>, #timeout=5, #size=5, #mon_waiting_queue=[]>
> user = User.first
=> #<User id: 8325205, firstname: "goryo", middlename: nil, creeated_at: "2012-03-20 18:02:58", updated_at: "2012-03-20 18:02:58">
> user.things
then i get these errors:
ActiveRecord::ConnectionNotEstablished: ActiveRecord::ConnectionNotEstablished
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:374:in `retrieve_connection'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:168:in `retrieve_connection'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:142:in `connection'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/alias_tracker.rb:75:in `connection'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/alias_tracker.rb:54:in `initial_count_for'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/alias_tracker.rb:12:in `initialize'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/alias_tracker.rb:29:in `call'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/alias_tracker.rb:29:in `default'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/alias_tracker.rb:29:in `[]'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/alias_tracker.rb:29:in `aliased_name_for'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/alias_tracker.rb:17:in `aliased_table_for'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/join_helper.rb:15:in `construct_tables'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/join_helper.rb:14:in `each'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/join_helper.rb:14:in `construct_tables'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/association_scope.rb:37:in `add_constraints'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/association_scope.rb:31:in `scope'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/association.rb:98:in `association_scope'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/association.rb:87:in `scoped'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/collection_association.rb:380:in `find_target'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/collection_association.rb:333:in `load_target'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/collection_proxy.rb:44:in `__send__'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/collection_proxy.rb:44:in `load_target'
from /usr/lib/ruby/gems/1.8/gems/activerecord-3.2.2/lib/active_record/associations/collection_proxy.rb:87:in `method_missing'
from /usr/lib/ruby/1.8/irb.rb:310:in `output_value'
from /usr/lib/ruby/1.8/irb.rb:159:in `eval_input'
from /usr/lib/ruby/1.8/irb.rb:271:in `signal_status'
from /usr/lib/ruby/1.8/irb.rb:155:in `eval_input'
from /usr/lib/ruby/1.8/irb.rb:154:in `eval_input'
from /usr/lib/ruby/1.8/irb.rb:71:in `start'
from /usr/lib/ruby/1.8/irb.rb:70:in `catch'
from /usr/lib/ruby/1.8/irb.rb:70:in `start'

ohh all I needed was to establish ActiveRecord::Base connection as an initial inorder to connect to other tables, it works!
ActiveRecord::Base.establish_connection :my_connection
since my basedb is an abstract class, when you establish a connection ActiveRecord::Base w/c is the superclass doesnt have a connection yet you can check it out
>BaseDB.load_configs
>BaseDB.establish_connection :my_connection
try to query
>User.all
>BaseDB.connected?
=>returns true
>ActiveRecord::Base.connected?
=>nil
for more info about the implementation you can check this out http://api.rubyonrails.org/classes/ActiveRecord/Base.html

Related

RoR - rake rs:index fails with NameError on Rails 4.0.4(4.1.7), thinking sphinx 3.1.1

I tried to index my mysql database for sphinx search engine and that's what I get. I went through the whole entire stackoverflow, downgraded my rails, updated sphinx and ts like 5 or 6 times. I double checked my code(maybe problem's over there). I depreciated config.threadsafe! and whatever else and it still doesn't work at all. Probably you could help me out. Here are console logs and parts of the original code:
pchudinov:xxx chud$ rake ts:index
/Library/Ruby/Gems/2.0.0/gems/bundler-1.7.4/lib/bundler/runtime.rb:222: warning: Insecure world writable dir /usr/local in PATH, mode 040777
DEPRECATION WARNING: config.threadsafe! is deprecated. Rails applications behave by default as thread safe in production as long as config.cache_classes and config.eager_load are set to true. (called from <class:Application> at /xxx/config/application.rb:29)
searchd is not currently running.
Stopped searchd daemon (pid: ).
Generating configuration to /xxx/config/development.sphinx.conf
rake aborted!
NameError: uninitialized constant RealTeams
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.0.2/lib/active_support/inflector/methods.rb:226:in `const_get'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.0.2/lib/active_support/inflector/methods.rb:226:in `block in constantize'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.0.2/lib/active_support/inflector/methods.rb:224:in `each'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.0.2/lib/active_support/inflector/methods.rb:224:in `inject'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.0.2/lib/active_support/inflector/methods.rb:224:in `constantize'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.0.2/lib/active_support/core_ext/string/inflections.rb:66:in `constantize'
/Library/Ruby/Gems/2.0.0/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/core/index.rb:43:in `model'
/Library/Ruby/Gems/2.0.0/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/index.rb:9:in `append_source'
/Library/Ruby/Gems/2.0.0/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/interpreter.rb:63:in `__source'
/Library/Ruby/Gems/2.0.0/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/interpreter.rb:20:in `indexes'
/xxx/app/indices/real_teams_index.rb:2:in `block in <top (required)>'
/Library/Ruby/Gems/2.0.0/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/core/interpreter.rb:3:in `translate!'
/Library/Ruby/Gems/2.0.0/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/core/index.rb:39:in `interpret_definition!'
/Library/Ruby/Gems/2.0.0/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/active_record/index.rb:32:in `sources'
/Library/Ruby/Gems/2.0.0/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/configuration/consistent_ids.rb:31:in `collect'
/Library/Ruby/Gems/2.0.0/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/configuration/consistent_ids.rb:31:in `sources'
/Library/Ruby/Gems/2.0.0/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/configuration/consistent_ids.rb:19:in `attributes'
/Library/Ruby/Gems/2.0.0/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/configuration/consistent_ids.rb:23:in `sphinx_internal_ids'
/Library/Ruby/Gems/2.0.0/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/configuration/consistent_ids.rb:7:in `reconcile'
/Library/Ruby/Gems/2.0.0/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/configuration.rb:87:in `render'
/Library/Ruby/Gems/2.0.0/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/configuration.rb:96:in `block in render_to_file'
/Library/Ruby/Gems/2.0.0/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/configuration.rb:96:in `open'
/Library/Ruby/Gems/2.0.0/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/configuration.rb:96:in `render_to_file'
/Library/Ruby/Gems/2.0.0/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/rake_interface.rb:13:in `configure'
/Library/Ruby/Gems/2.0.0/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/rake_interface.rb:24:in `index'
/Library/Ruby/Gems/2.0.0/gems/thinking-sphinx-3.1.1/lib/thinking_sphinx/tasks.rb:9:in `block (2 levels) in <top (required)>'
Tasks: TOP => ts:index
(See full trace by running task with --trace)
Here is the original code for "RealTeam":
controller:
def search
#real_teams = RealTeam.search params[:search]
end
model:
class RealTeam < ActiveRecord::Base
has_many :players
has_many :home_matches, :class_name => 'Match', :foreign_key => 'team_home_id'
has_many :away_matches, :class_name => 'Match', :foreign_key => 'team_away_id'
has_many :performances
end
index:
ThinkingSphinx::Index.define :real_teams, :with => :active_record do
indexes :name
end
P.S. I am not that common with rails yet, but as far as I think code's done properly.
Your index definition should refer to :real_team (singular, like the model name) instead of :real_teams.

one to many relationship ruby on rails

So I made changes to my changes to subject.rb file here
class CreateSubjects < ActiveRecord::Migration
def up
create_table :subjects do |t|
t.string "name"
t.integer "position"
t.boolean "visible", :default =>false
t.timestamps
end
end
def down
drop_table :subjects
end
end
and then I went ahead and defined page.rb file in
class Page < ActiveRecord::Base
belongs_to :subject
end
Then I got to the terminal
Muhammeds-MacBook-Pro:simple_cms muhammedz786$ rails c
Loading development environment (Rails 4.0.2)
irb(main):001:0> subject = Subject.find(1)
Subject Load (0.3ms) SELECT subjects.* FROM subjects WHERE subjects.id = 1 LIMIT 1
=> #
Then I tried to call the subject.page method and I got the follwoing mistake! What does this mean?
irb(main):002:0> subject.page
**Mysql2::Error: Table 'simple_cms_development.pages' doesn't exist: SHOW FULL FIELDS FROM `pages`**
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'simple_cms_development.pages' doesn't exist: SHOW FULL FIELDS FROM `pages`
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:287:in `query'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:287:in `block in execute'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract_adapter.rb:435:in `block in log'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activesupport-4.0.2/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract_adapter.rb:430:in `log'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:287:in `execute'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/mysql2_adapter.rb:222:in `execute'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:301:in `execute_and_free'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:462:in `columns'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/schema_cache.rb:114:in `block in prepare_default_proc'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/schema_cache.rb:56:in `yield'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/schema_cache.rb:56:in `columns'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/schema_cache.rb:118:in `block in prepare_default_proc'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/schema_cache.rb:67:in `yield'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/schema_cache.rb:67:in `columns_hash'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/association_scope.rb:25:in `column_for'
... 3 levels...
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/association_scope.rb:44:in `each_with_index'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/association_scope.rb:44:in `add_constraints'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/association_scope.rb:19:in `scope'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/association.rb:103:in `association_scope'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/association.rb:87:in `scope'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/singular_association.rb:42:in `find_target'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/association.rb:147:in `load_target'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/association.rb:54:in `reload'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/singular_association.rb:9:in `reader'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/activerecord-4.0.2/lib/active_record/associations/builder/association.rb:70:in `page'
from (irb):2
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
from /Users/muhammedz786/.rbenv/versions/2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
Make sure that your Pages model has a subject_id in the migration, and then make sure to run rake db:migrate

Alias attriibute for column names in Rails

So, I have a db which was setup for an old php app and I cannot touch the way the DB is done.
However, I have some column name in the db that are using some restricted words in Ruby like "class"
So to make it work, I used alias_attribute.
But it's not working. Here is the code:
class Contractor < ActiveRecord::Base
set_table_name "contractors"
alias_attribute :class_type, :class
attr_accessible :about, :alias, :business_entity, :business_logo, :business_name, :city, :class, :county, :created, :email, :founded, :metroarea, :nid, :phone, :state, :status, :street_address, :uid, :zipcode
end
here are the errors:
1.9.3-p125 :001 > Contractor.last
SyntaxError: /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.13/lib/active_model/attribute_methods.rb:345: syntax error, unexpected ')', expecting '='
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.13/lib/active_model/attribute_methods.rb:343:in `module_eval'
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.13/lib/active_model/attribute_methods.rb:343:in `define_optimized_call'
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.13/lib/active_model/attribute_methods.rb:227:in `block in alias_attribute'
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.13/lib/active_model/attribute_methods.rb:224:in `each'
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.13/lib/active_model/attribute_methods.rb:224:in `alias_attribute'
from /Users/snoopy/Projects/buildzoom/ruby/service_request/app/models/contractor.rb:3:in `<class:Contractor>'
from /Users/snoopy/Projects/buildzoom/ruby/service_request/app/models/contractor.rb:1:in `<top (required)>'
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:469:in `load'
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:469:in `block in load_file'
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:639:in `new_constants_in'
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:468:in `load_file'
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:353:in `require_or_load'
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:502:in `load_missing_constant'
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:192:in `block in const_missing'
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:190:in `each'
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/activesupport-3.2.13/lib/active_support/dependencies.rb:190:in `const_missing'
from (irb):1
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
from /Users/snoopy/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
and
1.9.3-p125 :003 > Contractor.where(:zipcode => "94111")
Contractor Load (648.6ms) SELECT `contractors`.* FROM `contractors` WHERE `contractors`.`zipcode` = '94111'
(Object doesn't support #inspect)
=>
The last one only appear when I remove the alias_attribute otherwise it's the same error as first one
I think your problem is the alias name you gave ":class"
class is one of the ActiveRecord methods
you can see it by running this command
Contractor.first.methods
defining this alias simply "override" it in some way and interrupt to rails operation
So, there is the gem safe_attributes who is doing the work.
It's working perfectly so far.

Can't access model attributes using has many through rails query

I have a has many through association that I have included below. I can retrieve the record, but cannot access any of the attributes by a direct call. Below are the results of
Lead.first.case_details
Lead.first.case_details.case_number
I think i should note that all my attr_accessible are :as => admin. If that is causing the problem then i am just looking for a secure way to get and set the attributes so that the users cannot
Lead.first.case_details
Lead Load (0.0ms) SELECT "leads".* FROM "leads" LIMIT 1
CaseDetail Load (0.0ms) SELECT "case_details".* FROM "case_details" INNER JOIN "case_detail_leads" ON "case_details"."id" = "case_detail_leads"."case_detail_id" WHERE "case_detail_leads"."lead_id" = 1
[#<CaseDetail id: 1, case_number: "131", style_of_case: "V", case_type: "CC", judge: "DIAL", date_filed: "2013-03-17 05:00:00", first_charge_id: nil, first_lead_id: 1, location_id: nil, legalscraper_id: nil, created_at: "2013-03-30">]
Lead.first.case_details.case_number
Lead Load (1.0ms) SELECT "leads".* FROM "leads" LIMIT 1
#<Class:0x5d351d8>: undefined method `case_number' for #<ActiveRecord::Relation:0x5d360d8>
from D:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.12/lib/active_record/relation/delegation.rb:45:in `method_missing'
from D:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.12/lib/active_record/associations/collection_proxy.rb:100:in `method_missing'
from (irb):69
from D:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.12/lib/rails/commands/console.rb:47:in `start'
from D:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.12/lib/rails/commands/console.rb:8:in `start'
from D:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.12/lib/rails/commands.rb:41:in `<top (required)>'
from E:/Sites/aws/script/rails:6:in `require'
from E:/Sites/aws/script/rails:6:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
Models
class Lead < ActiveRecord::Base
has_many :case_detail_leads
has_many :case_details, :through => :case_detail_leads
class LeadOffense < ActiveRecord::Base
belongs_to :lead
belongs_to :offense
class Offense < ActiveRecord::Base
has_many :lead_offenses
has_many :leads, :through => :lead_offenses
Lead.first.case_details results as an Array which doesn't have a methode case_number. What you can do:
Lead.first.case_details.first.case_number
Lead.first.case_details.map{|case_detail| case_detail.case_number}

DataMapper No Memory Error - Sinatra

Any ideas as to where I am going wrong here?
Error:
C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/dm-do-adapter-1.1.0/lib/dm-do-adapter/adapter.rb:291:in `next!': failed to allocate memory (NoMemoryError)
from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/dm-do-adapter-1.1.0/lib/dm-do-adapter/adapter.rb:291:in `select_fields'
from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/dm-do-adapter-1.1.0/lib/dm-do-adapter/adapter.rb:39:in `select'
from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/dm-do-adapter-1.1.0/lib/dm-do-adapter/adapter.rb:276:in `with_connection'
from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/dm-do-adapter-1.1.0/lib/dm-do-adapter/adapter.rb:33:in `select'
from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/dm-migrations-1.1.0/lib/dm-migrations/adapters/dm-mysql-adapter.rb:99:in `show_variable'
from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/dm-migrations-1.1.0/lib/dm-migrations/adapters/dm-mysql-adapter.rb:89:in `character_set'
from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/dm-migrations-1.1.0/lib/dm-migrations/adapters/dm-mysql-adapter.rb:54:in `create_table_statement'
from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/dm-migrations-1.1.0/lib/dm-migrations/adapters/dm-do-adapter.rb:94:in `create_model_storage'
from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/dm-do-adapter-1.1.0/lib/dm-do-adapter/adapter.rb:276:in `with_connection'
from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/dm-migrations-1.1.0/lib/dm-migrations/adapters/dm-do-adapter.rb:93:in `create_model_storage'
from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/dm-migrations-1.1.0/lib/dm-migrations/adapters/dm-do-adapter.rb:57:in `upgrade_model_storage'
from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/dm-migrations-1.1.0/lib/dm-migrations/auto_migration.rb:71:in `upgrade_model_storage'
from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/dm-migrations-1.1.0/lib/dm-migrations/auto_migration.rb:143:in `auto_upgrade!'
from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/dm-migrations-1.1.0/lib/dm-migrations/auto_migration.rb:45:in `send'
from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/dm-migrations-1.1.0/lib/dm-migrations/auto_migration.rb:45:in `repository_execute'
from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/dm-core-1.1.0/lib/dm-core/support/descendant_set.rb:66:in `each'
from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/dm-core-1.1.0/lib/dm-core/support/subject_set.rb:212:in `each'
from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/dm-core-1.1.0/lib/dm-core/support/ordered_set.rb:321:in `each'
from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/dm-core-1.1.0/lib/dm-core/support/ordered_set.rb:321:in `each'
from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/dm-core-1.1.0/lib/dm-core/support/subject_set.rb:212:in `each'
from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/dm-core-1.1.0/lib/dm-core/support/descendant_set.rb:65:in `each'
from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/dm-migrations-1.1.0/lib/dm-migrations/auto_migration.rb:44:in `repository_execute'
from C:/RailsInstaller/Ruby1.8.7/lib/ruby/gems/1.8/gems/dm-migrations-1.1.0/lib/dm-migrations/auto_migration.rb:27:in `auto_upgrade!'
from list.rb:51
Code:
require 'rubygems'
require 'sinatra'
require 'omniauth/oauth'
require 'rest-graph'
require 'hashie/hash'
require 'dm-core'
require 'dm-serializer/to_json'
require 'dm-migrations'
require 'dm-timestamps'
require 'dm-validations'
require 'dm-mysql-adapter'
enable :sessions
APP_ID = "XXXXX"
APP_SECRET = "XXXX"
SQLCONN = "mysql://XXXXXXX"
DataMapper.setup(:default, SQLCONN)
class Person
include DataMapper::Resource
property :id, Serial
property :FBid, String
property :visits, Integer
property :first_visit, DateTime
property :recent_visit, DateTime
property :token, String
has n, :links
end
class Link
include DataMapper::Resource
property :id, Serial
property :created, DateTime
property :deactivated, DateTime
property :active, Boolean
belongs_to :person
def self.currentLinks
all(:active == true)
end
end
DataMapper.auto_upgrade!
all(:active == true)
try...
all(:active => true)