[Rails] How to ensure two tables reference the same record in a third table?

2009-09-25 Thread Patrick Doyle

class Part < ActiveRecord::Base
  has_many :lots
  has_many :components # or, maybe not
end

class Lot < ActiveRecord::Base
  belongs_to :part
  has_many :components
end

class Component < ActiveRecord::Base
  belongs_to :part # or, maybe not
  belongs_to :lot
end

In my domain, a "lot" of "components" has a part number (which I
represent in RoR as a #belongs_to relationship, even though the term
"belongs_to" is somewhat misleading).  Each component in the lot has a
part number which is the same as the part number for the entire lot.

What is the best way to ensure that this consistency is maintained?
That is, that all parts in a lot reference the same part number as the
lot itself.

One way to ensure the consistency is to remove the "part_id" column
from the component table and to reference a components part number
through the lot association, i.e. mycomponent.lot.part.number.  The
problem is, sometimes I want to look at all of the parts that have a
given part number, and the only way I can see to do that is to collect
all of the lots with a given part number and then collect all of the
components in each of the lots, using something like:
mypart.lots.map(&:components).flatten.  Unfortunately, that returns an
array of components instead of the dynamic finder that would be
returned by mypart.components.

A second way to ensure the consistency is to add validations to the
Lot and/or Component models.  I could do that, but I have been advised
elsewhere that validations are best when augmented by database
constraints.  (See
http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/3b023e81df58619d,
if you are interested).  Is there some database agnostic way to add
this sort of constraint?

Or is there some other mechanism to implement this?

--wpd

--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~--~~~~--~~--~--~---



[Rails] how to ensure

2009-04-15 Thread daociyiyou


 how do i get my app database info only after another server has
called my app's method and
 updated my app's database? Now, i can just get the database info
before anther server calls my app's method.

def one
  some code
  two
  some code
end

def two
 calls another server's methods and sends my app ping url(http://
myhost/.../three) that will be automaticlly called by that server
after executing the two method
end

def three
  another server calls this method and updates database
end
--~--~-~--~~~---~--~~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~--~~~~--~~--~--~---