[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

create rails model classes dynamically ?

uncle

9/19/2006 4:28:00 PM

Rails has a very nice database migration utility, where you can write
something like this...

class CreateHouse < ActiveRecord::Migration

def self.up
create_table :house do |t|
t.column 'person_id', :integer, :default => nil, :null => false
t.column "created_at", :datetime
t.column "name", :string, :limit => 128, :default => ""
end
end

def self.down
drop_table :place
end

end

Typically, you then create a class in the model directory that looks
like this...

class House < ActiveRecord::Base

belongs_to :person

end


I'd like to dynamicallly generate this House class. With the help of
this post, I can create the class and methods dynamically...
http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/b969da2473e2c713/b8d076423378a2f2?lnk=gst&q=create+class&rnum=8#b8d076...
But I have not been able to figure out how to add the belongs_to and
has_many style calls at the class level to the dynamicall generated
class. Also, once this class is initially created dynamically,
depending on what tables are created later, there may have to be
additional belongs_to and has_many calls injected into the existing
class, whew.

-- any help would be great, thanks, Andrew

5 Answers

e

9/19/2006 4:37:00 PM

0

unknown wrote:
> Rails has a very nice database migration utility, where you can write
> something like this...

You will get better answers over on Rails lists.

http://groups.google.com/group/rubyon...

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

William Crawford

9/19/2006 5:34:00 PM

0

unknown wrote:
> I'd like to dynamicallly generate this House class. With the help of
> this post, I can create the class and methods dynamically...

http://magicmodels.ruby...

I'm not into Rails yet, but I came across this the other day and tagged
it as potentially useful in the future. I suspect it's what you want.

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

uncle

9/19/2006 7:14:00 PM

0

wow...bulls eye !

Exactly what I am looking for.

Thanks again Internet

-- Andrew

William Crawford wrote:
> unknown wrote:
> > I'd like to dynamicallly generate this House class. With the help of
> > this post, I can create the class and methods dynamically...
>
> http://magicmodels.ruby...
>
> I'm not into Rails yet, but I came across this the other day and tagged
> it as potentially useful in the future. I suspect it's what you want.
>
> --
> Posted via http://www.ruby-....

Varun Goel

4/24/2008 3:25:00 PM

0

unknown wrote:
> Rails has a very nice database migration utility, where you can write
> something like this...
>
> class CreateHouse < ActiveRecord::Migration
>
> def self.up
> create_table :house do |t|
> t.column 'person_id', :integer, :default => nil, :null => false
> t.column "created_at", :datetime
> t.column "name", :string, :limit => 128, :default => ""
> end
> end
>
> def self.down
> drop_table :place
> end
>
> end
>
> Typically, you then create a class in the model directory that looks
> like this...
>
> class House < ActiveRecord::Base
>
> belongs_to :person
>
> end
>
>
> I'd like to dynamicallly generate this House class. With the help of
> this post, I can create the class and methods dynamically...
> http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/b969da2473e2c713/b8d076423378a2f2?lnk=gst&q=create+class&rnum=8#b8d076...
> But I have not been able to figure out how to add the belongs_to and
> has_many style calls at the class level to the dynamicall generated
> class. Also, once this class is initially created dynamically,
> depending on what tables are created later, there may have to be
> additional belongs_to and has_many calls injected into the existing
> class, whew.
>
> -- any help would be great, thanks, Andrew


Hi,
U said that u r able to create class dynamically i used same link but
i m not able to create class dynamically please help me with some code
snippet.
Please help me i am facing this problem from last two weeeks??

Thanks
varun
--
Posted via http://www.ruby-....

Simon Krahnke

4/24/2008 5:30:00 PM

0

* Varun Goel <varun.rajeshkumar@gmail.com> (17:24) schrieb:

> Hi,
> U said that u r able to create class dynamically i used same link but
> i m not able to create class dynamically please help me with some code
> snippet.
> Please help me i am facing this problem from last two weeeks??

You can create a class by Class.new. For example:

c = Class.new do
def moo
"moo!"
end
end

obj = c.new
puts obj.moo

mfg, simon .... l