Hello everyone, I''m working on a Rails 3 social networking app (following RailsSpace by Addison Wesley) but the book is old and using Rails 2. Thus far I''ve been chugging along converting it along the way, but I''m running into some trouble with my Avatar model. I''m trying to implement it without having a table in the database. In accordance to the book, I created the model manually (instead of with generate) and add an initialize function. I created the corresponding controller and views, but when I try to upload my avatar, it errors out telling me that database.avatars doesn''t exist. I''m pretty sure I''ve got the controller and model right, but I just need to tell Rails to NOT look in the database for this model... Any ideas? Thanks in advance, - Jeff -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
All the classes inside ''models'' folder are considered as ActiveRecord classes and Rails will try to find a corresponding table in the database named as plural of the model. It''s a convention; To bypass it and have a model independent from ActiveRecord class, create it in ''lib'' folder and require it in the class where you need it. On Feb 25, 8:38 am, Jeff Miller <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hello everyone, > I''m working on a Rails 3 social networking app (following RailsSpace > by Addison Wesley) but the book is old and using Rails 2. Thus far I''ve > been chugging along converting it along the way, but I''m running into > some trouble with my Avatar model. I''m trying to implement it without > having a table in the database. In accordance to the book, I created the > model manually (instead of with generate) and add an initialize > function. I created the corresponding controller and views, but when I > try to upload my avatar, it errors out telling me that database.avatars > doesn''t exist. > > I''m pretty sure I''ve got the controller and model right, but I just need > to tell Rails to NOT look in the database for this model... > > Any ideas? > > Thanks in advance, > - Jeff > > -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
That''s actually not true. If you don''t specify them as active record base subclasses they''ll be just regular ruby classes. Blog: http://random8.zenunit.com/ Twitter: http://twitter.com/random8r Learn: http://sensei.zenunit.com/ New video up now at http://sensei.zenunit.com/ real fastcgi rails deploy process! Check it out now! On 25/02/2011, at 7:12 PM, Javix <s.cambour-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> All the classes inside ''models'' folder are considered as ActiveRecord > classes and Rails will try to find a corresponding table in the > database named as plural of the model. It''s a convention; To bypass it > and have a model independent from ActiveRecord class, create it in > ''lib'' folder and require it in the class where you need it. > > On Feb 25, 8:38 am, Jeff Miller <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote: >> Hello everyone, >> I''m working on a Rails 3 social networking app (following RailsSpace >> by Addison Wesley) but the book is old and using Rails 2. Thus far I''ve >> been chugging along converting it along the way, but I''m running into >> some trouble with my Avatar model. I''m trying to implement it without >> having a table in the database. In accordance to the book, I created the >> model manually (instead of with generate) and add an initialize >> function. I created the corresponding controller and views, but when I >> try to upload my avatar, it errors out telling me that database.avatars >> doesn''t exist. >> >> I''m pretty sure I''ve got the controller and model right, but I just need >> to tell Rails to NOT look in the database for this model... >> >> Any ideas? >> >> Thanks in advance, >> - Jeff >> >> -- >> Posted viahttp://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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi, Jeff! Chap #2 of this Crafting Rails Applications (http://www.pragprog.com/ titles/jvrails/crafting-rails-applications) might help... Regards, jb On Feb 25, 7:38 am, Jeff Miller <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hello everyone, > I''m working on a Rails 3 social networking app (following RailsSpace > by Addison Wesley) but the book is old and using Rails 2. Thus far I''ve > been chugging along converting it along the way, but I''m running into > some trouble with my Avatar model. I''m trying to implement it without > having a table in the database. In accordance to the book, I created the > model manually (instead of with generate) and add an initialize > function. I created the corresponding controller and views, but when I > try to upload my avatar, it errors out telling me that database.avatars > doesn''t exist. > > I''m pretty sure I''ve got the controller and model right, but I just need > to tell Rails to NOT look in the database for this model... > > Any ideas? > > Thanks in advance, > - Jeff > > -- > Posted viahttp://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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org 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.
On 25 February 2011 08:12, Javix <s.cambour-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> All the classes inside ''models'' folder are considered as ActiveRecord > classes and Rails will try to find a corresponding table in the > database named as plural of the model. It''s a convention; To bypass it > and have a model independent from ActiveRecord class, create it in > ''lib'' folder and require it in the class where you need it.I don''t believe that is true. I understood that as long as the model does not derive from ActiveRecord then it should not look in the database. Colin -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 25 Feb 2011, at 10:27, Colin Law wrote:>> All the classes inside ''models'' folder are considered as ActiveRecord >> classes and Rails will try to find a corresponding table in the >> database named as plural of the model. It''s a convention; To bypass >> it >> and have a model independent from ActiveRecord class, create it in >> ''lib'' folder and require it in the class where you need it. > > I don''t believe that is true. I understood that as long as the model > does not derive from ActiveRecord then it should not look in the > database.Indeed it''s not true, you can have any class in the models directory. It''s up to you as a developer to decide whether it belongs there. In our applications, it''s quite common we have modellike behavior in plain Ruby classes. Heck, with Rails 3, being able to mix in ActiveModel modules in a non-AR class should make it very obvious that a model isn''t necessarily a one-on-one with a database table. Best regards Peter De Berdt -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Fri, Feb 25, 2011 at 1:38 AM, Jeff Miller <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hello everyone, > I''m working on a Rails 3 social networking app (following RailsSpace > by Addison Wesley) but the book is old and using Rails 2. Thus far I''ve > been chugging along converting it along the way, but I''m running into > some trouble with my Avatar model. I''m trying to implement it without > having a table in the database. In accordance to the book, I created the > model manually (instead of with generate) and add an initialize > function. I created the corresponding controller and views, but when I > try to upload my avatar, it errors out telling me that database.avatars > doesn''t exist. > >This is because chapter 12 of that book tells you to create your Avatar model like so: class Avatar < ActiveRecord::Base When you inherit from ActiveRecord::Base it checks to see that you have a table in the database that matches Avatar. Back when the book was written I''m sure the rules of having a table were not as strict so the author inherited from ActiveRecord::Base as a short cut for not writing his own error handling (see page 378 section 12.2.3).> I''m pretty sure I''ve got the controller and model right, but I just need > to tell Rails to NOT look in the database for this model... > > Any ideas? >Yes. As others have suggested correctly you do not need to inherit from ActiveRecord::Base in order to have a class in the models folder. Remove < ActiveRecord::Base from the top of your Avatar class. That will take care of it not looking into the database anymore for an Avatar table. After that you will need to write your own error handling for the areas where you were using AR''s build in error handling. You''ll see them pretty quick when you run your code. The will most likely show up as Undefined Method errors for "errors.add_to_base" and so on. B. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 25 Feb 2011, at 15:30, Bryan Crossland wrote:> I''m pretty sure I''ve got the controller and model right, but I just > need > to tell Rails to NOT look in the database for this model... > > Any ideas? > > Yes. As others have suggested correctly you do not need to inherit > from ActiveRecord::Base in order to have a class in the models > folder. Remove < ActiveRecord::Base from the top of your Avatar > class. That will take care of it not looking into the database > anymore for an Avatar table. After that you will need to write your > own error handling for the areas where you were using AR''s build in > error handling. You''ll see them pretty quick when you run your code. > The will most likely show up as Undefined Method errors for > "errors.add_to_base" and so on.Or you could just "include ActiveModel::Validations" in your Avatar class and get both validations and error handling. Best regards Peter De Berdt -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
You should look into ActiveModel http://railscasts.com/episodes/219-active-model This will allow you to create model that has certain ActiveRecord like capabilities like validations without the need for a database. Look at the mail_form gem (https://github.com/plataformatec/mail_form/blob/master/lib/mail_form/shim.rb) for another example. (The mail_form gem is described in detail in the ''Crafting Rails Applications'', previously mentioned by JB.) Jeff Miller wrote in post #983799:> > I''m pretty sure I''ve got the controller and model right, but I just need > to tell Rails to NOT look in the database for this model... >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Wow, thanks for the responses everybody! I''ll be working on this a little later today, I''ll definitely try out your suggestions. Thanks!! - Jeff -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.