Rails 3 Association NoMethodError - ruby-on-rails

I am relatively new to Rails and am a little bit in the Deep end.
I am working in Rails 3 with a pre-existing MSSQL DB and having to retrofit out the models to fit.
I seem to have created my models without too many issues but have hit an issue with associations.
Here's my Schema for the two tables in question
create_table "ip_addresses", :id => false, :force => true do |t|
t.integer "id", :null => false
t.integer "computer_id", :null => false
t.string "ip", :limit => 64, :null => false
t.string "ip_subnet", :limit => 64, :null => false
t.datetime "timestamp", :null => false
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "computers", :id => false, :force => true do |t|
t.integer "id", :null => false
t.string "uid", :limit => 128
t.string "enclosure_uid", :limit => 128
t.string "name", :limit => 128
t.integer "status_id", :null => false
t.integer "designation_id", :null => false
t.integer "hardware_type_id", :null => false
t.string "hardware_model", :limit => 128
t.string "processor_model", :limit => 128
t.integer "processor_speed"
t.integer "processor_cores_per_proc", :limit => 1
t.integer "processor_count"
t.decimal "memory", :precision => 9, :scale => 2
t.decimal "physical_disk", :precision => 9, :scale => 2
t.integer "san_disk"
t.string "os_name", :limit => 128
t.string "os_version", :limit => 128
t.string "os_patchlevel", :limit => 128
t.integer "campus_id", :null => false
t.text "description"
t.datetime "modification_date", :null => false
t.datetime "os_installdate"
t.datetime "created_at"
t.datetime "updated_at"
end
Here are the two models
class Computer < ActiveRecord::Base
has_many :ip_addresses
end
class IpAddress < ActiveRecord::Base
belongs_to :computer
end
It seems like the Association is working when I use the IpAddress Class, but not from the Computer class.
see below.
>> IpAddress.first.computer.name
"GC-PRD-PS02"
>> Computer.first.ip_addresses.ip
NoMethodError: undefined method `ip' for #<Class:0x10291cb18>
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.10/lib/active_record/base.rb:1014:in `method_missing'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.10/lib/active_record/associations/association_collection.rb:444:in `send'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.10/lib/active_record/associations/association_collection.rb:444:in `method_missing'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.10/lib/active_record/base.rb:1127:in `with_scope'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.10/lib/active_record/associations/association_proxy.rb:207:in `send'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.10/lib/active_record/associations/association_proxy.rb:207:in `with_scope'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-3.0.10/lib/active_record/associations/association_collection.rb:440:in `method_missing'
from (irb):40
What is strange is that I can see the entire row object of the IpAddress Class by using
>> Computer.first.ip_addresses
[#<IpAddress id: 175, computer_id: 687, ip: "10.0.246.80", ip_subnet: "255.255.255.192", timestamp: "2011-08-03 11:17:57", created_at: "2011-10-07 01:06:16", updated_at: "2011-10-07 01:06:16">, #<IpAddress id: 176, computer_id: 687, ip: "192.168.234.235", ip_subnet: "255.255.255.255", timestamp: "2011-08-03 11:17:57", created_at: "2011-10-07 01:06:16", updated_at: "2011-10-07 01:06:16">, #<IpAddress id: 177, computer_id: 687, ip: "192.168.159.1", ip_subnet: "255.255.255.0", timestamp: "2011-08-03 11:17:57", created_at: "2011-10-07 01:06:16", updated_at: "2011-10-07 01:06:16">, #<IpAddress id: 178, computer_id: 687, ip: "192.168.42.1", ip_subnet: "255.255.255.0", timestamp: "2011-08-03 11:17:57", created_at: "2011-10-07 01:06:16", updated_at: "2011-10-07 01:06:16">]
I have search quite a bit to try and work this one out. I'm sure it's just my way of doing associations. Any help would be much Appreciated.
Thanks,
Rob

As Computer.first.ip_addresses is an array, you need to do :
Computer.first.ip_addresses.first.ip
if you want the fist ip
Computer.first.ip_addresses.map(&:ip)
if you want all ips

Calling ip on a collection - that looks like the problem. Computer.first.ip_addresses[0].ip should work (presuming ip_addresses contains at least one element).
Also for learning, note what puts Computer.first.ip_addresses.class gives you.

Related

gem annotate, error:Unable to annotate app/models/blog_post.rb: undefined method `supports_foreign_keys?'

ruby 2.0.0p247
Rails 3.2.22
mysql
I added gem 'annotate'
after bundle install. gem was installed
Next step: rails g annotate:install
next step: run rake db:migrate
Display error:
Unable to annotate app/models/blog_post.rb: undefined method `supports_foreign_keys?' for ActiveRecord::ConnectionAdapters::Mysql2Adapter:0x0000000346aae8
Similarly for other models
What wrong? How solve this issue. Thank you.
EDIT
shema.rb
.........................
create_table "blog_posts", :force => true do |t|
t.integer "user_id", :null => false
t.string "subject"
t.text "body", :null => false
t.datetime "created_at"
t.datetime "updated_at"
t.integer "comments_count", :default => 0, :null => false
t.boolean "delta", :default => true, :null => false
t.string "tags_line"
t.string "commentable_by", :default => "all", :null => false
t.string "visible_by", :default => "all", :null => false
t.integer "article_id"
t.boolean "draft", :default => false, :null => false
t.datetime "published_at"
t.datetime "last_comment_at"
end
add_index "blog_posts", ["delta"], :name => "index_blog_posts_on_delta"
add_index "blog_posts", ["user_id"], :name => "index_blog_posts_on_user_id"
........................................................................
Probably the versions of your annotate-gem and your mysql2-gem are not compatible. You are still using Rails 3.2. Maybe it is because your mysql2-gem is quite old.
When looking at the release notes of the annotate-gem, you can see that it had added foreign_key support in 2.6.9. So it can help to downgrade it to 2.6.8

students and teachers in a class_instruction

Setting up the database, I am just curious if I did it correctly, as it looks a bit off. There are people, who have a user account, and a role (teacher or student). they are participants in a class (where a class has many students and teachers; a student has many classes; a teacher has many classes). I think my class_instruction model is off in the DB, but please tell me if it will work, or if there is a better way (like maybe with a has_many_through table of participants)
schema.rb:
ActiveRecord::Schema.define(:version => 20130524160107) do
create_table "class_instructions", :force => true do |t|
t.string "name"
t.datetime "time"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "person_id"
end
add_index "class_instructions", ["person_id"], :name => "index_class_instructions_on_person_id"
create_table "people", :force => true do |t|
t.string "firstName"
t.string "lastName"
t.integer "user_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "people", ["user_id"], :name => "index_people_on_user_id"
create_table "roles", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
create_table "user_roles", :force => true do |t|
t.integer "user_id"
t.integer "role_id"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
add_index "user_roles", ["role_id"], :name => "index_user_roles_on_role_id"
add_index "user_roles", ["user_id"], :name => "index_user_roles_on_user_id"
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :default => "", :null => false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
end
My concern is that the person_id is part of the class. Is this correct?
ClassInsturction.rb :
class ClassInstruction < ActiveRecord::Base
belongs_to :people
has_many :cassignments
has_many :assignments, :through => :cassignments
def className
self.name
end
def classAssignments
return self.cassignments
end
end
I would make a joining table that has both class and people and use has many through. If people have many classes and classes have many people you can not do this any other way.
I have seen usually belongs_to with singular form:
belongs_to :person
I am not sure if this is what you are asking for though.

Rails scaffold strange undefined_method error for specific table

I'm currently working on an admin tool for an existing database and encountered a strange problem when scaffolding a particular table.
Here is the schema of the table using rake db:schema:dump:
create_table "locality", :force => true do |t|
t.integer "version", :limit => 8, :null => false
t.string "truth_id", :null => false
t.string "truth_record_id", :null => false
t.binary "deleted", :limit => 1, :null => false
t.string "internal_code", :null => false
t.string "iso_code"
t.string "materialized_path", :null => false
t.string "invariant_name", :null => false
t.binary "published", :limit => 1, :null => false
t.float "geo_point_latitude", :default => 0.0
t.float "geo_point_longitude", :default => 0.0
t.string "class", :null => false
t.integer "hut_count", :default => 0
t.integer "hotel_count", :default => 0
t.string "destination_url"
end
add_index "locality", ["truth_record_id"], :name => "truth_record_id", :unique => true
I used the schema_to_scaffold gem to create my scaffold from the dumped schema:
rails g scaffold locality version:integer truth_id:string truth_record_id:string
deleted:binary internal_code:string iso_code:string materialized_path:string
invariant_name:string published:binary geo_point_latitude:float
geo_point_longitude:float class:string hut_count:integer hotel_count:integer
destination_url:string
This workflow worked for a lot of other tables but when accessing /localities or Locality.all in the rails console all i get its:
irb(main):001:0> Locality.all
Locality Load (2.1ms) SELECT `locality`.* FROM `locality`
NoMethodError: undefined method `attribute_method_matcher' for "Country":String
Where does "Country":String come from?
At first I thought the model name 'locality' is somehow reservers by rails for i18n stuff but the same problem happens when naming the model 'Bla'.
I'm using rails 3.2.13 and a MySQL Database.
I believe that your column: class is invalid. How would you have access to that column since the class is already a method of any object in ruby?
I think that this causes the mess. The class column's value of your loaded locality is "Country" right?
So the problem was column named class, which ruby obviously hates.
Solution posted in this StackOverflow question: Legacy table with column named "class" in Rails
or more specifically in this blog post (Accessed 25.03.2013):
http://kconrails.com/2011/01/28/legacy-database-table-column-names-in-ruby-on-rails-3/

Rails comment out migration file for development?

I'm running this migration file on local and running into problems because part is MySQL which does not work because I'm using Sqlite3. I want to make sure the SQL runs only in production not in development. I understand I need to wrap part of this with this comment so that I can successfully run this migration:
unless Rails.env == "development".
How can I add this to the script below?
class Places < ActiveRecord::Migration
def self.up
create_table :countries do |t|
t.string :name, :limit => 50, :null => false
t.string :fips104, :limit => 2, :null => false
t.string :iso2, :limit => 2, :null => false
t.string :iso3, :limit => 3, :null => false
t.string :ison, :limit => 4, :null => false
t.string :internet, :limit => 2, :null => false
t.string :capital, :limit => 25
t.string :map_reference, :limit => 50
t.string :nationality_singular, :limit => 35
t.string :nationaiity_plural, :limit => 35
t.string :currency, :limit => 30
t.string :currency_code, :limit => 3
t.integer :population
t.string :title, :limit => 50
t.string :comment, :limit => 255
t.timestamps
end
create_table :regions do |t|
t.references :country, :null => false
t.string :name, :limit => 45, :null => false
t.string :code, :limit => 8, :null => false
t.string :adm1code, :limit => 4, :null => false
t.timestamps
end
create_table :cities do |t|
t.references :country, :null => false
t.references :region, :null => false
t.string :name, :limit => 45, :null => false
t.float :latitude, :null => false
t.float :longitude, :null => false
t.string :timezone, :limit => 10, :null => false
t.integer :dma_id
t.string :county, :limit => 25
t.string :code, :limit => 4
t.timestamps
end
add_index :cities, :name
execute "LOAD DATA INFILE '#{RAILS_ROOT}/db/migrate/Countries.txt' INTO TABLE countries
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES;"
execute "LOAD DATA INFILE '#{RAILS_ROOT}/db/migrate/Regions.txt' INTO TABLE regions
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES;"
execute "LOAD DATA INFILE '#{RAILS_ROOT}/db/migrate/Cities.txt' INTO TABLE cities
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES;"
end
def self.down
drop_table :countries
drop_table :regions
drop_table :cities
end
end
In your self.up method you can check environment like below:
if Rails.env.production?
#your mysql code which runs only in production env
end

Why aren't all tags returned?

Given a user model and a brand model, and acts_as_taggable_on I have 2 users that have tagged a brand with 2 tags each. Then in the console:
>> b = Brand.last
=> #<Brand id: 37, name: "herp", user_id: nil>
>> b.users.count
=> 2
>> b.tags
=> [#<ActsAsTaggableOn::Tag id: 24, name: "firsttag">, #<ActsAsTaggableOn::Tag id: 25, name: "lasttag">]
Those are the tags for Brand b only from the first user. The tags from the last user aren't showing. Why?
Edit: Using rails 3.0.9
Brand has_many tags, has_many brand_users and has_many users through brand_users
User has_many :brand_users and has_many :brands, :through => :brand_users
BrandUser belongs_to :brand and belongs_to :user
Below is my schema:
ActiveRecord::Schema.define(:version => 20110824083919) do
create_table "brand_users", :force => true do |t|
t.integer "brand_id"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "brands", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.string "user_id"
end
create_table "taggings", :force => true do |t|
t.integer "tag_id"
t.integer "taggable_id"
t.string "taggable_type"
t.integer "tagger_id"
t.string "tagger_type"
t.string "context"
t.datetime "created_at"
end
add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id"
add_index "taggings", ["taggable_id", "taggable_type", "context"], :name => "index_taggings_on_taggable_id_and_taggable_type_and_context"
create_table "tags", :force => true do |t|
t.string "name"
end
create_table "users", :force => true do |t|
t.string "provider"
t.string "uid"
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
end
I wasn't using the gem properly. Going through acts_as_taggable_on's core.rb file you find all_tags_on(context). Using b.tags calls all tags without a tagger (as seen in the SQL query) so using the following gives the answer for all tags with a tagger_type or tagger_id:
b.all_tags_on(:tags)

Resources