Anonymous Postings

Added by Andrew Stone 208 days ago

The forums have been opened up to anonymous postings.


Replies

How classy-inheritance works? - Added by Anonymous 207 days ago

Hi Andrew,

I'm a rails noob and still having some problems with this associations things (multi-model form, polymorphism, etc.)...
So I've discovered your gem that looks really helpful to me. But, don't know if it's my high noobness, the thing is I don't get how this really works.
The depends_on method it's used instead of belongs_to? Or I put it on the has_many place?

Help me with this example:
I have 4 models, User, Real state, Address and Cost.
The associations are:
- User has many Real_states
- User has one Address as addressable
- Real_state has one Address as addressable
- Real_state has many Costs

So, the problem is when I create a Real_state I wanna pass an Address and it's Costs too, in the same form. When creating a User I will pass the Address in the same form.
Think that I can use depends_on between Real_state and Costs. With the polymorphics can_be may be useful, but to me the name can be don't seems to fit (Real_state and User can not be Address, they have an address).

How can I use your classy-inheritance in this case?

RE: How classy-inheritance works? - Added by Anonymous 207 days ago

Sorry...forgot to introduce myself...

My name is Philipe Farias and I'm from Brazil (sorry for the bad english too).

RE: Anonymous Postings - Added by Andrew Stone 207 days ago

Hello Philipe,

First, your English is fine, no worries.

Classy Inheritance only works in a has_one or belongs_to relationship, there is no support for a has_many relationship (at this time). So, it won't be able to help you in the real_state has_many costs relationship. :(

For the others, do you have an addressable_type and addressable_id in the addresses table?

The can_be would be on the Address since it is the polymorphic class. Think of it like this: address can be user or address can be real_state. I've found can_be only useful in specific cases, so you may not need it.

The belongs_to would be on the User and RealState:

class User < ActiveRecord::Base
  belongs_to :address, :attrs => [:line_one, :line_two], :as => addressable
end
** I'm assuming your Address table has the columns line_one and line_two.

Classy inheritance is really new, so let me know if this works or not. It hasn't been exposed to a lot of use cases so it probably needs some additional feature support.

RE: Anonymous Postings - Added by Philipe Farias 207 days ago

Man this is the force of the community...super fast and quality support for quality solutions!

I kinda discovered myself that for has_many it doesn't work so well...but until your answer I was thinking that was my fault.
So for the has_many it's the standard Rails way.

In the polymorphic case the problem I see it's the direction of the association...It's more common (at least in the books and blogs examples that I have read) that a polymorphic model belongs to different models in different ways.
For example: post has many comments as commentables and author has one comment as commentable (in this case it's comment that belongs to others).

Dunno...for me it's a little weird that the polymorphic model have this other models, and in this case I think can_be it's hard to read also, but like you said, it's for specific cases.

It's in your plans to make something like has_one :address, :attrs => [:street, :city, :state], :as => :addressable (and for the has_many too)?

And that was something I noted that may cause some problem...I saw in the html generated that the attribute passed in the depends_on association don't have a prefix or other *fix that make it different.
In a case with this models:
- Person (with description attribute) depends_on picture, :attrs => [:description]
- Picture (with a description too)
How the framework knows the difference between them (the person description and the picture description)?

I'll keep watching, maybe sooner the specific case happens to me. And later I'll see your lockdown gem too (this also looks promise to me).

Keep your good work!

Philipe Farias

RE: Anonymous Postings - Added by Andrew Stone 207 days ago

Thanks for the kind words Philipe.

When you use depends_on with a polymorphic association (use :as => :addressable), it is setting up a has_one relationship behind the scenes. If the relationship is not polymorphic (no :as parameter used), the the relationship is a belongs_to.

The goal of Classy Inheritance is to use the conventions Rails provides to minimize the developer effort. Classy Inheritance will not override (monkey patch) any core code. That just leads to possible maintenance issues.

So basically, Classy Inheritance only does what you could do in your model, it just saves you the time.

About the problem with models having the same attribute, I already have plans for a :prefix attribute, so in your Person, Picture example above you could do:


class Person < ActiveRecord::Base
  depends_on :picture, :attrs => [:description], :prefix => true
end

so instead of the ambiguous (and non-working) @person.description you would get @person.picure_description

thanks for the comments,
andy