Polymorphic association

Hi, I want to do a polymorphic association like that:

class User
has_many :my_posts, :as => :owner
end

class Post
belongs_to, :owner, :polymorphic => true
end

meaning that I want to do:
@user = User.new
@user.my_posts (and that would return Post objects)

also
@post = Post.new
@post.owner (this would return a User object)

How can I do that?

Thanks in advance.

On Fri, Jan 27, 2012 at 11:44 AM, Rodrigo R. [email protected]
wrote:

meaning that I want to do:
@user = User.new
@user.my_posts (and that would return Post objects)

also
@post = Post.new
@post.owner (this would return a User object)

How can I do that?

It looks like you’re describing a standard has_many relationship. Are
you implying that Post will be extended to be different classes?


Greg A.
http://twitter.com/akinsgre

On Fri, Jan 27, 2012 at 11:52 AM, Greg A. [email protected]
wrote:

On Fri, Jan 27, 2012 at 11:44 AM, Rodrigo R. [email protected] wrote:

Hi, I want to do a polymorphic association like that:

Try this railscast


Greg A.
http://twitter.com/akinsgre

I did, and it goes like:

@commentable.comments

but I want something like:

@commentable.my_comments

and not change the Comment model name

I want this because I’m going to have to has_many associations to
comments
(to follow to same example):

class User
has_many :my_comments
has_many :comments_that_i_follow
end

On Fri, Jan 27, 2012 at 12:05 PM, Rodrigo R. [email protected]
wrote:

I did, and it goes like:

@commentable.comments

but I want something like:

@commentable.my_comments

and not change the Comment model name

def my_comments
return comments
end


Greg A.
http://twitter.com/akinsgre

You can use :class_name and :foreign_key if that’s what you’re looking
for.

Dheeraj K.

Sure does :smiley:

Dheeraj K.

Yes, exactly! Does that :class_name works for has_many?

I’ll try that now anyway.

Thank you

That didn’t work.

class Post
belongs_to :owner, :polymorphic => true
end

class User
has_many :my_posts, :class_name => ‘Post’, :as => :owner
end

I can’t do: Post.first.owner or User.first.my_posts

You might want to take a look at the rails guides on active record
associations

2012/1/27 Rodrigo R. [email protected]

Hey, I just manage to do what I wanted, but I had to add a owner_type
column on my Posts migration with the content:
:owner_type => ‘User’

Thanks for everyone, and is there a better way than saving another
varchar(255) to my database?

I did, also, it gives me the error: “SQLite3::SQLException: no such
column:
protests.owner_type:”

On Fri, Jan 27, 2012 at 4:37 PM, Benjamin Iandavid R. <

I suggest you pick up a book and start following a tutorial. Otherwise
you’ll spend most of your time here waiting for people to answer your
questions. This becomes frustrating very quickly.

Trust me, it may seem easier to come here and ask extremely simple
questions (the answers of which are obvious in the docs) but it’s not.
In
the long term, you’ll be thankful you took the extra few minutes to read
and understand the docs. The books are extremely helpful and will answer
most of your basic questions. I started learning Ruby and RoR about two
weeks ago, and I’m very glad I took the time to read through and
complete a
tutorial.

Just visit amazon.com and type rails books and you’ll have a great
selection to choose from. Also, try this, which I started with:
http://ruby.railstutorial.org/chapters/static-pages --avoid the
shortcuts
and don’t rush. It pays off in the end.

I read the Rails Tutorial book, but I does not teach anything related to
customizing that much, thats why I’m constantly search google and asking
questions here (when I don’t find it on google).

I’m still searching google, but couldn’t find my answer for the current
question.

On Fri, Jan 27, 2012 at 5:50 PM, Mohamad El-Husseini
<[email protected]

Could you recommend me a more complete book that I can read so I
understand
the basics?
I really feel like I don’t right now.

Thank you.

On Fri, Jan 27, 2012 at 7:12 PM, Mohamad El-Husseini
<[email protected]

Well, frankly, if I were you, I would read the documentation again. It
doesn’t seem like you know what a polymorphic association is. You
described
a standard has_many relationship.

Polymorphic association is when one model belongs to several different
models. For example, to use the Rails casts episode, a comment can
belong
to a Photo, a Post, or a Product.

From what you have written so far, it seems like you want a standard
has_many and belongs_to association.

User has_many :posts will let you do @user.posts, and a Post belongs_to
:user will let you do post.user. If you want customize the name of the
association, as in post.author, then I think you need to use the
:class_name

Don’t let that daunt you. I was in the same boat as you were not long
ago.
I’ll tell you what I did:

Completed http://ruby.railstutorial.org/ – twice! It does an awesome
job
of explaining things.
Rails for Zombies I, a screen cast series on codeschool.com (I
think–you’ll have to google it).
Rails Casts
Rails Guides

I actually started with Programming Ruby 1.9, the Pragmatic Programmers
Guide. I didn’t finish it. I read enough to get the basics of Ruby
syntax.
Knowing the Ruby syntax is essential since, to me, it looked nothing
like
anything I’ve seen before. Bracket were often omitted, and so were
semi-colons, blocks, etc… understanding the syntax made understanding
easier.

After getting through 200 pages, I switched to Agile Web D.
with
Rails 4th Ed. Frankly, I thought it was a waste of money at that point.
It
just told you to do stuff without explaining much of what you were
doing.
Especially when it came to testing. I quit and switched to Rails
Tutorial.
I don’t think it’s beginner friendly.Rails Tutorial I thought was
brilliant. But it was hard to follow because of some issues. There was
no
mention that Heroku did not support SQLite for example. But the book
does
an awesome job of explaining the basics. Read it carefully. And it’s
free.

I’m planning on Reading Rails AntiPatterns and The Rails 3 Way next…

On 27 January 2012 21:18, Rodrigo R. [email protected] wrote:

Could you recommend me a more complete book that I can read so I understand
the basics?
I really feel like I don’t right now.

When you say you have read the rails tutorial book is that the one
that goes with railstutorial.org? If so then reading it is not
enough. Work right through it doing all the examples and so on. Make
sure you understand every line of code. You may think that the
application it develops is not what you want but that does not matter,
it will show you a wide range of techniques. Make sure that you have
installed the version of rails that matches that used by any tutorial
you use.

Colin

I’ll second what Colin said. I’ll also emphasize the need to “hold your
horses”. There’s nothing more frustrating than having to wade through a
ton
of stuff before you start building something. But it’s absolutely worth
it,
and it will save you a ton of time in the long run.

If I had spent 10% of my time reading documentation, I would have saved
the
other 90% that I wasted editing lines of code, scanning hurriedly over
tutorials, asking a ton of questions, etc… it really is true. It’s
frustrating at first, but it pays off and saves you a ton of time in the
long term.