Hi, I''m a Java and ColdFusion developer trying to find my feet with JRuby and Rails. I''ve created a very simple "Auction" app, with a database schema like this: create_table "auction_items", :force => true do |t| t.string "description" t.string "title" t.datetime "start_date" t.datetime "end_date" t.datetime "created_at" t.datetime "updated_at" end create_table "bids", :force => true do |t| t.integer "auction_item_id" t.decimal "amount", :precision => 15, :scale => 2 t.datetime "bid_time" t.datetime "created_at" t.datetime "updated_at" end There are two classes in my model: auction_item.rb: class AuctionItem < ActiveRecord::Base has_many :bids end bid.rb: class Bid < ActiveRecord::Base belongs_to :auction_item end Initially, I had bid.rb like this: class Bid < ActiveRecord::Base belongs_to :AuctionItem end (ie. using the camel-cased class name, as per the definintion in the class rather than the underscored one), but it didn''t work until I changed it to use the undscored one. What is the rule here? Does the name in the "belongs_to" refer to the table name or something? Similarly, I''m a bit confused why I have to go to http://localhost:8080/auction_items (not http://localhost:8080/AuctionItems). Can anyone explain the general rules to me, or point me to somewhere that does please? Many thanks, Andrew. -- Posted via http://www.ruby-forum.com/.
Hi The first parameter to belongs_to is the association id So here auction_item_id being the foreign key according to convension by rails we can say belongs_to :auction_item It will be taken as auction_item_id For example you can try t.belongs_to :auction_item instead of t.integer "auction_item_id" in the above migration Defintion of belongs_to you can see at rails/activerecord/lib/active_record/associations.rb, line 1001 Sijo -- Posted via http://www.ruby-forum.com/.
Marnen Laibow-Koser
2009-Aug-07 14:01 UTC
Re: Newbie Confusion re underscores / camel case
Andrew Myers wrote: [...]> Can anyone explain the general rules to me, or point me to somewhere > that does please?Standard Ruby convention (should be explained, at least in part, in the Pickaxe Book). Class names (like all constants) are uppercase. Everything else is lowercase. Unless you are using a literal class name (e.g. AuctionItem.find), you probably want lowercase. The situation with has_many is a little odd, since the symbol isn''t directly *anything*: for example, in has_many :auction_items, notice that the name is plural. This is Rails trying to do friendly English-like syntax.> > Many thanks, > Andrew.Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
Andrew Myers wrote:> > Can anyone explain the general rules to me, or point me to somewhere > that does please? > > Many thanks, > Andrew.take a look at: ActiveSupport::CoreExtensions::String::Inflections best way is to load it up in irb and play with it''s methods or look it up on the rails api.. it will end all your confusion guaranteed. -- Posted via http://www.ruby-forum.com/.