开发者

Associations confusion

开发者 https://www.devze.com 2023-02-03 21:56 出处:网络
Scenario: Example: A seller decides to sell a BMW. Before selling a particular model of the BMW (say a 7-series 760 Li) a CarSketch needs to be created by the seller. This CarSketch belongs to the BMW

Scenario:

Example:

A seller decides to sell a BMW. Before selling a particular model of the BMW (say a 7-series 760 Li) a CarSketch needs to be created by the seller. This CarSketch belongs to the BMW, in here we get specifications about the BMW. This C开发者_JAVA百科arSketch also has a quick summary of the physical information of the BMW ( length, etc ) represented as a Presentation.

Thus, If I want to sell a BMW. a model 540 and a model 760.

# car_sketch_for_model_540 = CarSketch obj
# presentation_for_model_540 = Presentation obj

# car_sketch_for_model_760 = CarSketch obj
# presentation_for_model_760 = Presentation obj

the seller approves and starts selling the BMW car, model 540 and 760. That is, the buyers sees a list of Presentations only.

Since the car can have many buyers, a buyer will only choose (buy) one model (presentation).

The CarSketch model is used by the seller for internal stuff. When browsing the site, the buyer can see the available models to buy because rails looks at the presentations through the CarSketch model.

For each buyer interested in a particular model (Presentation), a Buyer record is added pointing to a presentation.

I have a problem trying to figure out how to set up these associations on Rails. Basically I have 4 models. Let's call these Car, CarSketch, Presentation, and Buyer.

  1. Car can have many CarSketch's
  2. CarSketch contains a foreign key for one Presentation. There could be 2 rows under CarSketch referencing one Presentation, too.
  3. There can be any number of Presentation records.
  4. Buyer contains a foreign key for one Presentation. There could be 2 rows under Buyer referencing one Presentation, too.
  5. CarSketch and Buyer have foreign keys to reference Car.

Car->CarSketch->Presentation

Car->Buyer->Presentation

How do I express (associations) Presentation with CarSketch and Buyer?!?

Is this correct?

class CarSketch < ActiveRecord::Base    
    belongs_to :presentation
end

class Buyer < ActiveRecord::Base
    belongs_to :presentation
end

#what to do here?
class Presentation < ActiveRecord::Base
    has_many :car_sketches
    has_many :buyers
end

I don't care about the records created at Presentation, really.

I want to be able to have:

#assume presentation is a Presentation record...

CarSketch.presentation = defined
Buyer.presentation = some other presentation

also, CarSketch and Buyer could point to the same Presentation record.

Help is appreciated!

Personally, even though the current one works. It just doesn't make sense when I read it... CarSketch and Buyer are join tables... created by me since I need to hold more attributes for them...

It doesn't make sense that a CarSketch belongs to a Presentation because any number of CarSketch can have one presentation and a presentation doesn't have many CarSketch See what I'm saying?

Or is it normal to get confused like these when defining your own join tables as models and these referencing other models?

I'm not interested in using Presentation directly to retrieve CarSketch or Buyer.

UPDATE

Heavily edited to replace A B C D with the corresponding names.

OP, please check.

UPDATE*

# A = Car
# B = CarSketch
# C = Presentation

# D = Buyer

# 1) a Car can have many car_sketches.
# 2) a CarSketch has one Presentation.
# 3) a Car can also have many buyers.
# 4) a Buyer has one Presentation.

# Car.car_sketches[0].Presentation = some presentation...
# Car.buyers[0].Presentation = some presentation...

# CarSketch and Buyer can reference the same Presentation record. Different presentation records, too. What would you define under the Presentation model? T


Not an answer, but you can expand on this and see if it is a correct understanding. (Much easier to understand a story :) Maybe you take this and weave the store in your question and then I delete this "answer".

There's a bunch of Cars and a bunch of Buyers.
Then there is a Maserati.
Sally the Sketcher creates a CarSketch for the Maserati.
    Q: So, Maserati can every only have one sketch.
Now Matt the Manager creates a PowerPoint Presentation containing that sketch.
    Q: So, a presentation can only be for one sketch.
Now, Matt shows Richie Rich the PowerPoint presentation. Richie likes (and is linked to that presentation)
    Q: Now there could be many more buyers linked to that presentation (they all have been shown the presentation)
0

精彩评论

暂无评论...
验证码 换一张
取 消