Monday, July 12, 2010

[Rails] Re: Getting belongs_to and has_many working: good plan?

On Jul 12, 8:24 pm, Marnen Laibow-Koser <li...@ruby-forum.com> wrote:
> RichardOnRails wrote:
> > I just added these linkage statements to my Expense and Vendor models,
> > respectively.
>
> Which?  You neglected to say. 

Change the Expense model:: app\models\expense.rb
class Expense < ActiveRecord::Base
belongs_to :vendor # Added
end

Change the Vendor model:: app\models\ vendor.rb
class Vendor < ActiveRecord::Base
has_many :expenses # Added

def name
#{qbname} # QuickBooks name
end
end

> > webpage (http://www.railsforum.com/viewtopic.php?id=265).
>
> Why are you following tutorials from 2006, before Rails 1.0 even
> existed?

Because it was the simplest one I stumbled across.

> And what have you been doing all this time?  You've been posting on this
> list for a long time; have you really never looked at associations before?

Actually, I have read about them but I didn't think of them because I
started with
f.collection_select(:vendor, :vendor_id, ...
as the means of adding a vendor to a new expense.

But I knew adding actual vendor-names (rather than vendor-ids) to
expense records was poor design. So then I was focused on getting the
vendor-id out of that select in order to add it to the expense
record ... and so on until I took some time off to think of something
better. Then Active Record association popped in my head, so I'm back
on this problem.

>
> > 
> > That leads me to think that I need to:
> > 1. Create migration Add_Vendor_ID_to_Expense and then add column
> > vendor_id of type integer, or something like that.
> > 2. Create migration Add_Expense_ID_to_Vendor and then add column
> > expense_id of type integer, or something like that.
>
> You don't need both. 

Why not? And if not, does it matter which one I use?

> > 3. Create migration Create_Expense_Vendor_Table and then add
> > expense_id and vendor_id columns.
>
> You may not need this at all. 
Really. You mean Active Record is smart enough to decipher this from
the mere fact that I added those one-liners to the respective models?

> And it should all be in one migration class if it represents one
> conceptual change. 
I thought of that but decided to go with my first thought.

Again, best wishes,
Richard

--
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.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate