Forum: Ruby on Rails Any kind of rails setting / plugin for this?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
328f0bb678423fcea01ebe3b0edc74e6?d=identicon&s=25 Ben Johnson (benjohnson)
on 2006-12-31 06:48
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.
B0f09f622c913a13b9bfd332fca56563?d=identicon&s=25 Dan Manges (Guest)
on 2006-12-31 09:13
(Received via mailing list)
Ben Johnson 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 Manges
328f0bb678423fcea01ebe3b0edc74e6?d=identicon&s=25 Ben Johnson (benjohnson)
on 2006-12-31 09:20
Dan Manges wrote:
> Ben Johnson 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 Manges

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.
B0f09f622c913a13b9bfd332fca56563?d=identicon&s=25 Dan Manges (Guest)
on 2006-12-31 18:23
(Received via mailing list)
Ben Johnson 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 Manges
This topic is locked and can not be replied to.