Posts [ 1 to 20 of 27 ]

Hi,I'm starting with Rails and I'm developing an app which will sit over this existing model (http://imgur.com/ZuaCm). Although I might apply some changes in the model (legacy stuff, so that is my last option in fact), I'm finding it hard to implement it with Rail's Active Record.I have Item and Property models (many-to-many relation), whose relation has a Value (1 item has several properties, and a property has a value for that item). Now, to complicate things a little more, an hierarchy of several value types was defined (I've only drawn 1 column in DECIMAL_VALUE and TEXT_VALUE tables, but there are more columns and more tables like those).So, until now I've created an "ItemValuation", which will be the joint model for Item/Property/Value, and my ideia is to use "has/through" between items and properties. But I don't know how to design the Value part. The idea would be have the value stored in one of the child tables, and have it's ID in the joint table. I've also read about polymorphic associations, which might be used here in that hierarchy (not sure how to use this here).Another doubt I have is, if keeping this model, which resources need to be nested (item -> property -> value?).

I see nothing that needs a polymorphic relationship but then your post is not very clear at all as to the exact nature of your issue

What you want and what you need are too often not the same thing!When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.(Quote by me 15th July 2009)

I think you made it clear. I was trying to implement some hierarchy/inheritance there with Value and its childs TextValue/DecimalValue. However, I don't even need to call Value.all.Regarding other point, in terms of nested resources, I will only need to nest Property inside Item right? When adding a property to a certain item, I'll specify the property value at the same time and store all the information in the relation table.

The polymorphic I was thinking about was to refer to DecimalValue/TextValue as a "valuable", and keep that "valuable_id" on ItemPropertyValue model to refer to the specific table automatically (possibly without the Value model). But perhaps i'm just misunderstanding the usage of polymorphic associations.

Regarding other point, in terms of nested resources, I will only need to nest Property inside Item right? When adding a property to a certain item, I'll specify the property value at the same time and store all the information in the relation table.

The polymorphic I was thinking about was to refer to DecimalValue/TextValue as a "valuable", and keep that "valuable_id" on ItemPropertyValue model to refer to the specific table automatically (possibly without the Value model). But perhaps i'm just misunderstanding the usage of polymorphic associations.

Hhhmmm That's definitely a candidate for a polymorphic relationship.

What you want and what you need are too often not the same thing!When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.(Quote by me 15th July 2009)

I will try to setup this polymorphic association also. Regarding nested resources I now understand I only need to define Properties and Items routes, as properties are created outside the item context.

What you want and what you need are too often not the same thing!When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.(Quote by me 15th July 2009)

Item and property were correctly created, as well as the relation on ItemValuations table. However, DecimalValue record wasn't created and, therefore, its Id isn't present on the relation.Regarding DecimalValue.new on Rails console, is it normal the id = nil?

id, created_at and updated_at are only set once a record hits the database. In the case of ID's it's normally the relevant RDBMS database (primary_key auto_increment) that assigns the value and Rails (ActiveRecord) deals with the dates.In a rails consoleWhat does this get you?

What you want and what you need are too often not the same thing!When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.(Quote by me 15th July 2009)

Show me the output from the inspect's and the sql generated from the save.

Also run

rake test

from a command prompt in the root folder of your app and show me the results

What you want and what you need are too often not the same thing!When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.(Quote by me 15th July 2009)

I just spotted a problemYou have some code that does not follow rails conventions that really should be put straight relating to the propery valuation relationhips declared in the models. Change propertValuations to property_valuationsSo

Also note the singular property_valuation declaration on the has_one.Put that straight then see what happens.

How on earth did you end up with no tests defined? Rails generates them automatically for you?Also, what's the definition for the property valuation class?

Last edited by jamesw (2012-10-25 00:16:27)

What you want and what you need are too often not the same thing!When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.(Quote by me 15th July 2009)

What you want and what you need are too often not the same thing!When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.(Quote by me 15th July 2009)

ok, I'm up to my eyes in work right now. I'll code this up over the weekend and get back to you with a working solution

What you want and what you need are too often not the same thing!When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.(Quote by me 15th July 2009)

- Is this the best approach, what could be improved? - Can I avoid the need to save the item explicitly before the value?- Seems that I can't get the valuable from a PropertyValuation object (it returns nil), even if the record on the database is ok, with valuable_id and valuable_type correctly filled.

Sorry, I meant to pst earlier. You have the foreign keys the wrong way roundvalue should contain the foreign key for property valuation.Sort that out and your original approach will work.Either that, or change the has_one to a belongs_to

What you want and what you need are too often not the same thing!When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.(Quote by me 15th July 2009)