Any kind of rails setting / plugin for this?

First off, thanks for your help. I am having trouble here completely
understanding model relationships. Maybe you can shed some light on this
subject.

Let’s say I have 2 models:

Category
Product

Where a category has ONE product. Just for the sake of making my point.

Then we have 2 objects:

category1
product1

product1 belongs to category1. Then I do this:

product1.price
=> 20.00
product1.price = 100.00
=> 100.00
product1.category.product.price
=> 20.00

When visualizing the relationships between these 2 objects wouldn’t it
make more sense for that last line of code to return 100.00?

Is there a plugin or any kind of configuration that can make this
possible?

Thanks for your help.

Ben J. wrote:

product1.price
=> 20.00
product1.price = 100.00
=> 100.00
product1.category.product.price
=> 20.00

When visualizing the relationships between these 2 objects wouldn’t it
make more sense for that last line of code to return 100.00?

Looks like you’re just missing a save.

product1.price #=> 20.00
product1.price = 100.00 #=> 100.00
product1.save #=> true
product1.category.product.price #=> 100.00

If that does not work, the product could be cached in the category, so
try:
product1.category.product.reload.price

Dan M.

Dan M. wrote:

Ben J. wrote:

product1.price
=> 20.00
product1.price = 100.00
=> 100.00
product1.category.product.price
=> 20.00

When visualizing the relationships between these 2 objects wouldn’t it
make more sense for that last line of code to return 100.00?

Looks like you’re just missing a save.

product1.price #=> 20.00
product1.price = 100.00 #=> 100.00
product1.save #=> true
product1.category.product.price #=> 100.00

If that does not work, the product could be cached in the category, so
try:
product1.category.product.reload.price

Dan M.

Thanks for the help. The reason I made this post is because I was trying
to do that code excerpt in the “validate” method in the model. So I
really can’t add that save in there, otherwise I would cause an infinite
loop.

The problem I’m having is that some models rely on values from other
models to be valid. If I change that value I want to make sure all of
the other model’s objects are ok with it, but they all see the old
value, not the value in the object.

Ben J. wrote:

The problem I’m having is that some models rely on values from other
models to be valid. If I change that value I want to make sure all of
the other model’s objects are ok with it, but they all see the old
value, not the value in the object.

If you’re going to have models rely on other models to be valid, I
would approach it a little differently. If changing the price of a
product can make a category invalid, in the product validation I would
check for this and then error with something like “price cannot be set
to this because categories depend on having a product with a certain
price” (or however your relationships are set up).

I’ve done this before in an application using workflows. If a certain
model is using a workflow, the workflow cannot be set to inactive. So
the one model validates that it has an active workflow, but then the
workflow model validates that it cannot be set to inactive if other
models are using it. Hopefully that makes sense.

Dan M.