Ruby on Rails Thursday, March 29, 2012

Schema attached..

model summaries...

class Name < ActiveRecord::Base

attr_accessible :given, :gender, :position

has_many :user_names
has_many :users, :through => :user_names


class UserNames < ActiveRecord::Base
belongs_to :user
belongs_to :name


class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable,
:timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

has_many :user_names
has_many :names, :through => :user_names


DATA MIGRATION..

class CreateUserNames < ActiveRecord::Migration
def change
create_table :user_names do |t|
t.integer :name_id
t.integer :user_id
t.integer :position

t.timestamps
end
end
end

then

class RemoveUserIdFromName < ActiveRecord::Migration
def up
remove_column :names, :user_id
end

def down
add_column :names, :user_id, :integer
end
end


DATA MOVER SCRIPT...


require 'rubygems'

User.all.each do |u|

names = Name.find_all_by_user_id(u.id)

names.each do |n|
un = UserNames.new( :user_id => u.id, :name_id => n.id, :position =>
n.position )
un.save
end

end

So with all that in place and data mover run console gives me errors...

ruby-1.9.2-p290 :006 > User.first.names
User Load (1.4ms) SELECT "users".* FROM "users" ORDER BY
lower(username) ASC LIMIT 1
NameError: uninitialized constant User::UserName

(other error out cut)

What am I doing wrong ?
Do my model relationships and data mover script look ok?

Attachments:
http://www.ruby-forum.com/attachment/7202/schema.rb


--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment