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

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}

Related

ActiveModel::MissingAttributeError: can't write unknown attribute `user_id`

I am trying to implement a "comment" feature as part of my assignment for an project I am building.
Earlier in the course we created a comment table and had the Faker gem generate fake comments.
My instructions are as follows:
Comments must be associated with users, so add a user_id foreign key to the comments table. Remember to add an index to it too;
Update the User model so you can call user.comments, and the Comment model so you can call comment.user;
Modify the seeds.rb file to create valid comments when you run db:reset;
Initially I tried to run my rails generate commands but kept running into this error:
▶ rake db:migrate
== 20150508143445 CreateComments: migrating ===================================
-- create_table(:comments)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table "comments" already exists: CREATE TABLE "comments" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "description" text, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) /Users/jon/code/bloccit/db/migrate/20150508143445_create_comments.rb:3:in `change'
-e:1:in `<main>'
ActiveRecord::StatementInvalid: SQLite3::SQLException: table "comments" already exists: CREATE TABLE "comments" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "description" text, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
/Users/jon/code/bloccit/db/migrate/20150508143445_create_comments.rb:3:in `change'
-e:1:in `<main>'
SQLite3::SQLException: table "comments" already exists
/Users/jon/code/bloccit/db/migrate/20150508143445_create_comments.rb:3:in `change'
-e:1:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
So I discovered that I needed to delete the old table from the start of the course. I did this in the console.
ActiveRecord::Base.connection.execute("drop table comments")
It seemed to work. I was then able to run these in terminal.
▶ rake db:migrate
== 20150508143445 CreateComments: migrating ===================================
-- create_table(:comments)
-> 0.0015s
== 20150508143445 CreateComments: migrated (0.0016s) ==========================
== 20150508152354 DropComments: migrating =====================================
== 20150508152354 DropComments: migrated (0.0000s) ============================
▶ rails g migration AddCommentToUsers commed_id:integer:index
invoke active_record
create db/migrate/20150508155200_add_comment_to_users.rb
▶ rake db:migrate
== 20150508155200 AddCommentToUsers: migrating ================================
-- add_column(:users, :commed_id, :integer)
-> 0.0010s
-- add_index(:users, :commed_id)
-> 0.0012s
== 20150508155200 AddCommentToUsers: migrated (0.0023s) =======================
models/comment.rb
class Comment < ActiveRecord::Base
belongs_to :user
end
models/user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
has_many :posts
has_many :comments
# CarrierWave method for attribute functionality
mount_uploader :avatar, AvatarUploader
# These methods check the role of a user in the database
def admin?
role == 'admin'
end
def moderator?
role == 'moderator'
end
end
migrations/create_comments.rb
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.text :description
t.timestamps null: false
end
end
end
migrations/add_comment_to_users.rb
class AddCommentToUsers < ActiveRecord::Migration
def change
add_column :users, :commed_id, :integer
add_index :users, :commed_id
end
end
In my seeds.rb I have changed
# Create Comments
50.times do
Comment.create!(
# user: users.sample, # we have not yet associated Users with Comments
post: posts.sample,
description: Faker::Lorem.paragraph
)
end
to
# Create Comments
50.times do
Comment.create!(
user: users.sample,
description: Faker::Lorem.paragraph
)
end
Now when I run rake db:reset I get this error.
▶ rake db:reset
-- create_table("advertisements", {:force=>:cascade})
-> 0.0047s
-- create_table("answers", {:force=>:cascade})
-> 0.0010s
-- add_index("answers", ["question_id"], {:name=>"index_answers_on_question_id"})
-> 0.0014s
-- create_table("comments", {:force=>:cascade})
-> 0.0010s
-- create_table("posts", {:force=>:cascade})
-> 0.0010s
-- add_index("posts", ["topic_id"], {:name=>"index_posts_on_topic_id"})
-> 0.0010s
-- add_index("posts", ["user_id"], {:name=>"index_posts_on_user_id"})
-> 0.0014s
-- create_table("questions", {:force=>:cascade})
-> 0.0010s
-- create_table("summaries", {:force=>:cascade})
-> 0.0010s
-- add_index("summaries", ["post_id"], {:name=>"index_summaries_on_post_id"})
-> 0.0010s
-- create_table("topics", {:force=>:cascade})
-> 0.0036s
-- create_table("users", {:force=>:cascade})
-> 0.0015s
-- add_index("users", ["commed_id"], {:name=>"index_users_on_commed_id"})
-> 0.0009s
-- add_index("users", ["comment_id"], {:name=>"index_users_on_comment_id"})
-> 0.0012s
-- add_index("users", ["email"], {:name=>"index_users_on_email", :unique=>true})
-> 0.0015s
-- add_index("users", ["reset_password_token"], {:name=>"index_users_on_reset_password_token", :unique=>true})
-> 0.0017s
-- initialize_schema_migrations_table()
-> 0.0034s
rake aborted!
ActiveModel::MissingAttributeError: can't write unknown attribute `user_id`
/Users/jon/code/Bloccit/db/seeds.rb:45:in `block in <top (required)>'
/Users/jon/code/Bloccit/db/seeds.rb:44:in `times'
/Users/jon/code/Bloccit/db/seeds.rb:44:in `<top (required)>'
-e:1:in `<main>'
Tasks: TOP => db:setup => db:seed
(See full trace by running task with --trace)
Here is the full stack trace:
ActiveModel::MissingAttributeError: can't write unknown attribute `user_id`
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute.rb:138:in `with_value_from_database'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_set.rb:39:in `write_from_user'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods/write.rb:74:in `write_attribute_with_type_cast'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods/write.rb:56:in `write_attribute'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods/dirty.rb:96:in `write_attribute'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_methods.rb:373:in `[]='
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/associations/belongs_to_association.rb:83:in `replace_keys'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/associations/belongs_to_association.rb:14:in `replace'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/associations/singular_association.rb:17:in `writer'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/associations/builder/association.rb:123:in `user='
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_assignment.rb:54:in `public_send'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_assignment.rb:54:in `_assign_attribute'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_assignment.rb:41:in `block in assign_attributes'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_assignment.rb:35:in `each'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/attribute_assignment.rb:35:in `assign_attributes'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/core.rb:559:in `init_attributes'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/core.rb:281:in `initialize'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/inheritance.rb:61:in `new'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/inheritance.rb:61:in `new'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/persistence.rb:50:in `create!'
/Users/jon/code/Bloccit/db/seeds.rb:45:in `block in <top (required)>'
/Users/jon/code/Bloccit/db/seeds.rb:44:in `times'
/Users/jon/code/Bloccit/db/seeds.rb:44:in `<top (required)>'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `block in load'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/engine.rb:547:in `load_seed'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/tasks/database_tasks.rb:250:in `load_seed'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/railties/databases.rake:180:in `block (2 levels) in <top (required)>'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:240:in `call'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:240:in `block in execute'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:235:in `each'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:235:in `execute'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:179:in `block in invoke_with_call_chain'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:201:in `block in invoke_prerequisites'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:199:in `each'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:199:in `invoke_prerequisites'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:178:in `block in invoke_with_call_chain'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:165:in `invoke'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/railties/databases.rake:139:in `block (2 levels) in <top (required)>'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:240:in `call'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:240:in `block in execute'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:235:in `each'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:235:in `execute'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:179:in `block in invoke_with_call_chain'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/task.rb:165:in `invoke'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:150:in `invoke_task'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `block (2 levels) in top_level'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `each'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:106:in `block in top_level'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:115:in `run_with_threads'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:100:in `top_level'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:78:in `block in run'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:75:in `run'
/Users/jon/code/Bloccit/bin/rake:8:in `<top (required)>'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `block in load'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
/Users/jon/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
-e:1:in `<main>'
Tasks: TOP => db:setup => db:seed
I am stumped here. I thought by deleting the old table and running the new generate commands + migrating everything would be in order. Apparently this is not the case.
What am I doing wrong here?
edited to include line 45 of my seeds.rb
Comment.create!(
You have the wrong migration - instead of adding a user_id to comments you add users.commed.
Oops. It can happen to the best of us.
So first let's create a migration to clean up this mistake:
class RemoveCommedFromUsers < ActiveRecord::Migration
def change
remove_column :users, :commed # will also remove the index
end
end
Of course if the App has not been deployed you could just delete the offending migration and run rake db:reset
So lets create the correct migration
rails g migration AddUserToComments user:belongs_to
Which generates the following migration:
class AddUserToComments < ActiveRecord::Migration
def change
add_reference :comments, :user, index: true
end
end
add_reference creates a index and a foreign key in one sweep.

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.

activerecord 3.2.2 association issues with multiple connections

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