Errors about aggregation

Hey there

i dont quite know the change of rails 2.0 , but according to Agile
Development With Rails, aggregation seem not working…i now create a
class like this

class Temp
  attr_reader :n, :p

  def initialize(n,p)
    @n = n
    @p = p
  end

  def to_s
    [@n,@p].compact.join(",")
  end
end

now i create a class using ActiveRecord to save to Database, it looks
like this:

def User<ActiveRecord::Base
  #next line NO.17
  composed_of( :attr, :class_name=>Temp, :mapping=>[[:name , :n],
[:password , :p]])

  temp = Temp.new("abc","abc")
  User.create(:attr=> temp)
end

the problem is some ERROR prompt up :

d:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.0.2/lib/active_support/
dependencies.rb:478:in `const_missing': uninitialized constant
User::Temp (NameError)
  from test.rb:17

which indicates the <compose_of> method to be mistaken , i check the
rails API, but the the LATEST rails API is 2007 Dec on
http://www.rubyonrail.com
, is the API Rails 1.X ? Have not updated to rails 2.0 yet?

On 16 May 2008, at 10:53, stone wrote:

def initialize(n,p)
like this:

def User<ActiveRecord::Base
 #next line NO.17
 composed_of( :attr, :class_name=>Temp, :mapping=>[[:name , :n],

:class_name should be the name, ie :class_name => ‘Temp’
Also make sure that your Temp class is in temp.rb

User::Temp (NameError)
from test.rb:17

which indicates the <compose_of>  method to be mistaken , i check the
rails API, but the the LATEST rails API is 2007 Dec on http://www.rubyonrail.com
, is the API Rails 1.X ? Have not updated to rails 2.0 yet?

Dec 2007 is when rails 2.0 came out.

it’s all in test.rb before i write this , as your suggestion, i put
Temp class into temp.rb file,but still not working , still some
error :

test.rb:2:Warning: require_gem is obsolete.  Use gem instead.
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/
aggregations.rb:171:in `x=': undefined method `constantize' for
Temp:Class (NoMethodError)
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:2117:in `send'
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:2117:in `attributes='
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:2116:in `each'
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:2116:in `attributes='
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:1926:in `initialize'
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:571:in `new'
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:571:in `create'
  from test.rb:19

line 19 is <%User.create(:x=>temp)%>

On May 16, 5:57 pm, Frederick C. [email protected]

You just need modify this line
:mapping=>[[:name , :n], [:password , :p]]
to
:mapping => [[“name” , “n”], [“password” , “p”]]

On Fri, May 16, 2008 at 5:53 PM, stone [email protected] wrote:

def initialize(n,p)
like this:
the problem is some ERROR prompt up :


Nibirutech CTO Eric.Archangel
MSN: [email protected]
QQ: 996252
GMAIL: [email protected]
BLOG: http://blog.sina.com.cn/gameloft

it’s all in one test.rb before , now the Temp class has moved to
temp.rb , but still some errors ,why?

test.rb:2:Warning: require_gem is obsolete.  Use gem instead.
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/
aggregations.rb:171:in `attr=': undefined method `constantize' for
Temp:Class (NoMethodError)
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:2117:in `send'
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:2117:in `attributes='
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:2116:in `each'
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:2116:in `attributes='
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:1926:in `initialize'
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:571:in `new'
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:571:in `create'
  from test.rb:19

which indicates the line <% User.create(:attr=>temp) %> to be mistaken

On May 16, 5:57 pm, Frederick C. [email protected]

not working my friend , still the same error

it’s all in one test.rb before , now the Temp class has moved to
temp.rb , but still some errors ,why?

test.rb:2:Warning: require_gem is obsolete.  Use gem instead.
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/
aggregations.rb:171:in `attr=': undefined method `constantize' for
Temp:Class (NoMethodError)
        from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:2117:in `send'
        from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:2117:in `attributes='
        from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:2116:in `each'
        from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:2116:in `attributes='
        from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:1926:in `initialize'
        from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:571:in `new'
        from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:571:in `create'
        from test.rb:19

which indicates the line <% User.create(:attr=>temp) %> to be
mistaken

On May 16, 5:57 pm, Frederick C. [email protected]

Can you show us all of your Temp class please, and the code that you’re
using to get that error, and any other model that defines a relationship
with the Temp class. It looks possible to me that you’re doing
:class_name
=> Temp, when it should be :class_name => “Temp”. I think that’s where I
recall getting an error message like that from.


Appreciated my help?
Reccommend me on Working With Rails
http://workingwithrails.com/person/11030-ryan-bigg

Yup, just as I thought!

composed_of :attr, :class_name=>Temp, :mapping=>[[:name , :n],
[:password , :p]]

This should be :class_name=>“Temp”, and then it may work after that.

May I ask, what are you trying to achieve with this code? It doesn’t
seem
very Railsy.

Appreciated my help?
Reccommend me on Working With Rails
http://workingwithrails.com/person/11030-ryan-bigg

okay

this is temp.rb

class Temp
  attr_reader :n, :p
  def initialize(n,p)
    @n = n
    @p = p
  end
  def to_s
    [@n,@p].compact.join(",")
  end
end

and this is test.rb

require "rubygems"
require_gem "activerecord"
require "pp"
require 'Temp'

ActiveRecord::Base.establish_connection(:adapter=>"mysql",:host=>"localhost",:database=>"test",
:encoding=>"utf8",:username=>"root",:password=>"root")

class User < ActiveRecord::Base
  composed_of :attr, :class_name=>Temp, :mapping=>[[:name , :n],
[:password , :p]]

  temp = Temp.new("abc","abc")
  User.create(:attr => temp)
end

they are in the same fold ,my seql database have 3 columns , id , name
and password , id is unique and key , name and password can be null

On May 19, 1:39 pm, “Ryan B. (Radar)” [email protected]

sorry to say , that still not right after changing

test.rb:2:Warning: require_gem is obsolete.  Use gem instead.
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/
base.rb:2177:in `sort': undefined method `<=>' for :name:Symbol
(NoMethodError)
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:2177:in `attribute_names'
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:2458:in `clone_attributes'
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:2126:in `attributes'
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:2338:in `attributes_with_quotes'
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:2262:in `create_without_callbacks'
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/callbacks.rb:226:in `create_without_timestamps'
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/timestamp.rb:29:in `create'
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:2238:in `create_or_update_without_callbacks'
   ... 8 levels...
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/transactions.rb:120:in `rollback_active_record_state!'
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/transactions.rb:108:in `save'
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:572:in `create'
  from test.rb:19
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
On May 19, 1:54 pm, "Ryan B. (Radar)" <[email protected]>

oh no , not again , Railsy or not , anyway , i’m still learning

test.rb:2:Warning: require_gem is obsolete.  Use gem instead.
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/
base.rb:2177:in `sort': undefined method `<=>' for :name:Symbol
(NoMethodError)
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:2177:in `attribute_names'
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:2458:in `clone_attributes'
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:2126:in `attributes'
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:2338:in `attributes_with_quotes'
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:2262:in `create_without_callbacks'
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/callbacks.rb:226:in `create_without_timestamps'
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/timestamp.rb:29:in `create'
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:2238:in `create_or_update_without_callbacks'
   ... 8 levels...
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/transactions.rb:120:in `rollback_active_record_state!'
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/transactions.rb:108:in `save'
  from d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/
active_record/base.rb:572:in `create'
  from test.rb:19

On May 19, 1:54 pm, “Ryan B. (Radar)” [email protected]

in fact I just create a ruby project , include all the lib files ,
assuming same as a rails project and start my code.

On May 19, 2:20 pm, “Ryan B. (Radar)” [email protected]

finally, as Erik the Archangel said , i also change the mapping part ,
i works now , thank you very much for your help

No that’s not how you create a Rails app. You have to firstly generate
the
skeleton for the rails app. Read through the tutorial, it should teach
you a
few things.

On Mon, May 19, 2008 at 3:59 PM, stone [email protected] wrote:

Ok, do you want to make this more railsy? Your models don’t appear to be


Appreciated my help?
Reccommend me on Working With Rails
http://workingwithrails.com/person/11030-ryan-bigg

Where are you calling sort? It’s pointing to line 19 in your test.rb,
which
looks to me to be the call to create.

Ok, do you want to make this more railsy? Your models don’t appear to be
in
a rails directory at all, and the way you are establishing the
connection
and defining the model in the same file is not very neat. I suggest
reading
through a Rails tutorial, I’ve begun to write a large one myself and
that
should kick start you on the path to enlightenment:
http://frozenplague.net/forum-guide-getting-started/ Just click through
the
pages in the right menu, they are in the proper order, ignoring the
Frequently Asked Questions and Lovely People pages.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs