Hi, Consider this scenario, I have a table which stores the "likes" and it would stores the likes from all sorts of tables like blogs,comments, postings, reviews, etc. So the table has the following columns(id, like_id and type) where like_id is the id from any of the previous mentioned tables and type would be single char column to identify the foreign key table(B for blog, C for comments and so on). How am i supposed to create the relation between like table and the other tables? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Walter Lee Davis
2011-Aug-10 12:26 UTC
Re: A single column with foreign keys from Multiple tables
On Aug 9, 2011, at 2:09 PM, Rahul wrote:> Hi, > > Consider this scenario, I have a table which stores the > "likes" and it would stores the likes from all sorts of tables like > blogs,comments, postings, reviews, etc. So the table has the following > columns(id, like_id and type) where like_id is the id from any of the > previous mentioned tables and type would be single char column to > identify the foreign key table(B for blog, C for comments and so on). > How am i supposed to create the relation between like table and the > other tables? >Have a look at any of the polymorphic tagging libraries out there for an architectural model. The last time I did this, I used mbleigh-acts- as-taggable-on (Rails 2.3). But there were lots of other examples out there in that vein. Walter -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Matt Jones
2011-Aug-10 14:02 UTC
Re: A single column with foreign keys from Multiple tables
On Aug 9, 2:09 pm, Rahul <rahulratz...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > > Consider this scenario, I have a table which stores the > "likes" and it would stores the likes from all sorts of tables like > blogs,comments, postings, reviews, etc. So the table has the following > columns(id, like_id and type) where like_id is the id from any of the > previous mentioned tables and type would be single char column to > identify the foreign key table(B for blog, C for comments and so on). > How am i supposed to create the relation between like table and the > other tables?Check out the :polymorphic option to belongs_to - it does exactly what you''re describing, except for the (premature) "optimization" of storing a single character for the type. --Matt Jones -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi, Awesome guys.. Thanx a lot.. :polymorphic does exactly what I wanted... I''m a newbie in rails but I''m starting to love it.. -- Rahul On Aug 10, 7:02 pm, Matt Jones <al2o...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Aug 9, 2:09 pm, Rahul <rahulratz...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi, > > > Consider this scenario, I have a table which stores the > > "likes" and it would stores the likes from all sorts of tables like > > blogs,comments, postings, reviews, etc. So the table has the following > > columns(id, like_id and type) where like_id is the id from any of the > > previous mentioned tables and type would be single char column to > > identify the foreign key table(B for blog, C for comments and so on). > > How am i supposed to create the relation between like table and the > > other tables? > > Check out the :polymorphic option to belongs_to - it does exactly what > you''re describing, except for the (premature) "optimization" of > storing a single character for the type. > > --Matt Jones-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
i do it for rails 3 but i think that will work for you too in the migration... create_table :likes do |t| t.references :likeable, polimorphyc => true #with that, rails build likeable_id for the id of the others tables, and likeable_type for identify the table ..... .... end in like model belongs_to :likeable, :polymorphic => true ... in the other tables has_one :like, :as =>:likeable or has_many :like, :as =>:likeable take a look http://guides.rubyonrails.org/association_basics.htm On 10 ago, 14:26, Walter Lee Davis <wa...-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> wrote:> On Aug 9, 2011, at 2:09 PM, Rahul wrote: > > > Hi, > > > Consider this scenario, I have a table which stores the > > "likes" and it would stores the likes from all sorts of tables like > > blogs,comments, postings, reviews, etc. So the table has the following > > columns(id, like_id and type) where like_id is the id from any of the > > previous mentioned tables and type would be single char column to > > identify the foreign key table(B for blog, C for comments and so on). > > How am i supposed to create the relation between like table and the > > other tables? > > Have a look at any of the polymorphic tagging libraries out there for > an architectural model. The last time I did this, I used mbleigh-acts- > as-taggable-on (Rails 2.3). But there were lots of other examples out > there in that vein. > > Walter-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
i do it for rails 3 but i think that will work for you too in the migration... create_table :likes do |t| t.references :likeable, polimorphyc => true #with that, rails build likeable_id for the id of the others tables, and likeable_type for identify the table ..... .... end in like model belongs_to :likeable, :polymorphic => true ... in the other tables has_one :like, :as =>:likeable or has_many :like, :as =>:likeable take a look http://guides.rubyonrails.org/association_basics.htm On 10 ago, 14:26, Walter Lee Davis <wa...-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> wrote:> On Aug 9, 2011, at 2:09 PM, Rahul wrote: > > > Hi, > > > Consider this scenario, I have a table which stores the > > "likes" and it would stores the likes from all sorts of tables like > > blogs,comments, postings, reviews, etc. So the table has the following > > columns(id, like_id and type) where like_id is the id from any of the > > previous mentioned tables and type would be single char column to > > identify the foreign key table(B for blog, C for comments and so on). > > How am i supposed to create the relation between like table and the > > other tables? > > Have a look at any of the polymorphic tagging libraries out there for > an architectural model. The last time I did this, I used mbleigh-acts- > as-taggable-on (Rails 2.3). But there were lots of other examples out > there in that vein. > > Walter-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.