case study

33 views
Skip to first unread message

oto iashvili

unread,
Jan 27, 2013, 5:27:51 PM1/27/13
to rubyonra...@googlegroups.com
hi,

i' trying to re-create my project from php to rails. But now Im faced with a pb to witch I had solution with php, but I cant figure how to make it work with rails

here my tables

ads
  id
  category_id
  title
  text

ad_real_estate_details
  id
  ad_id
  nb_room
  floor

ad_car_details
  id
  ad_id
  color
  brand

here what I succeed to do


class Ad < ActiveRecord::Base
  attr_accessible :category_id :title, :text, :ad_real_estate_details
  has_one :ad_real_estate_details
  accepts_nested_attributes_for :ad_real_estate_details, allow_destroy: true
end

class AdRealEstateDetail < ActiveRecord::Base
  belongs_to :ad
  validates :ad_id, presence: true
end

but this only work for other category than real_estate

so I was thinking of polymorphism, but polymorphism mean I should add a reference in my "ad" table to the detail tables, and remove annonce_id from detail tables , what I thing is non-sense as some ads can have no details, but details are non-sense without an ad.

I was also thinking of a class AdDetails, and  AdRealEstateDetail would inherite from it, but this is not possible with rails as all subclasses will share the same table

does anyone have a solution for this kind of problem ?

thanks

Jim Ruther Nill

unread,
Jan 27, 2013, 7:10:30 PM1/27/13
to rubyonra...@googlegroups.com
i think you have it right when you mentioned using polymorphic associations.  it is fine to leave the polymorphic association
nil if the ad has no details if that's what you're worried about.
 

I was also thinking of a class AdDetails, and  AdRealEstateDetail would inherite from it, but this is not possible with rails as all subclasses will share the same table

does anyone have a solution for this kind of problem ?

thanks

--
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 rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/GLpFbbHm0ogJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
-------------------------------------------------------------
visit my blog at http://jimlabs.heroku.com

Nicolas Desprès

unread,
Jan 28, 2013, 5:18:37 AM1/28/13
to rubyonra...@googlegroups.com
On Sun, Jan 27, 2013 at 11:27 PM, oto iashvili <optimum...@laposte.net> wrote:
hi,

i' trying to re-create my project from php to rails. But now Im faced with a pb to witch I had solution with php, but I cant figure how to make it work with rails

here my tables

ads
  id
  category_id
  title
  text

ad_real_estate_details
  id
  ad_id
  nb_room
  floor

ad_car_details
  id
  ad_id
  color
  brand

here what I succeed to do


class Ad < ActiveRecord::Base
  attr_accessible :category_id :title, :text, :ad_real_estate_details
This should be :add_real_estate_details_attributes 
  has_one :ad_real_estate_details
  accepts_nested_attributes_for :ad_real_estate_details, allow_destroy: true
end

class AdRealEstateDetail < ActiveRecord::Base
  belongs_to :ad
  validates :ad_id, presence: true
end

but this only work for other category than real_estate

so I was thinking of polymorphism, but polymorphism mean I should add a reference in my "ad" table to the detail tables, and remove annonce_id from detail tables , what I thing is non-sense as some ads can have no details, but details are non-sense without an ad.

I was also thinking of a class AdDetails, and  AdRealEstateDetail would inherite from it, but this is not possible with rails as all subclasses will share the same table

does anyone have a solution for this kind of problem ?

thanks

--
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 rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/GLpFbbHm0ogJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Nicolas Desprès

oto iashvili

unread,
Jan 29, 2013, 6:41:43 AM1/29/13
to rubyonra...@googlegroups.com
Hi,

thanks for answer. My pb with polymorphic is that I would have to create first in db a ligne for ad_real_estate_details and then create my ad, what is non sense for me, in terms of logic.

lets have another example which is almost the same

image we have Student (age, classe, ...)  and Teacher (subject_taught) classes that inherit from User classes. I would be non sense to create first a Teacher entity, and then to link it to User, no ?

Jim Ruther Nill

unread,
Jan 29, 2013, 7:39:18 AM1/29/13
to rubyonra...@googlegroups.com
On Tue, Jan 29, 2013 at 7:41 PM, oto iashvili <optimum...@laposte.net> wrote:
Hi,

thanks for answer. My pb with polymorphic is that I would have to create first in db a ligne for ad_real_estate_details and then create my ad, what is non sense for me, in terms of logic.

lets have another example which is almost the same

image we have Student (age, classe, ...)  and Teacher (subject_taught) classes that inherit from User classes. I would be non sense to create first a Teacher entity, and then to link it to User, no ?

Why are you thinking of creating the details first?  Why not create the ad first, then add the details later? Sometimes, accepts_nested_attributes_for is not always the answer for creating association records.  Try to look into virtual attributes
and callbacks.
 
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.

To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/zT4pri8N4GUJ.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

oto iashvili

unread,
Jan 30, 2013, 5:48:10 AM1/30/13
to rubyonra...@googlegroups.com
I want to do 
@ad = Ad.attributes(params[:ad])
maybe Im wrong, but as I understood about polymorphic in rails, this would thow an error say that  ad_real_estate_details does not exist

Jim Ruther Nill

unread,
Jan 30, 2013, 9:27:43 AM1/30/13
to rubyonra...@googlegroups.com
On Wed, Jan 30, 2013 at 6:48 PM, oto iashvili <optimum...@laposte.net> wrote:
I want to do 
@ad = Ad.attributes(params[:ad])
maybe Im wrong, but as I understood about polymorphic in rails, this would thow an error say that  ad_real_estate_details does not exist


As I've said, don't use accepts_nested_attributes_for.  Create a getter and setter method for each kind of details that you want.
If you don't want to do that, create an attr_accessor for the details, and create the detail record in a callback.

 
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/DT2QhvrfLpUJ.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

oto iashvili

unread,
Jan 30, 2013, 6:08:10 PM1/30/13
to rubyonra...@googlegroups.com
hi

thanks for answer sorry I quite new in RoR. If I do how u said, will I have to validate "manually" all the attributes for details ?

Jim Ruther Nill

unread,
Jan 30, 2013, 8:07:54 PM1/30/13
to rubyonra...@googlegroups.com
On Thu, Jan 31, 2013 at 7:08 AM, oto iashvili <optimum...@laposte.net> wrote:
hi

thanks for answer sorry I quite new in RoR. If I do how u said, will I have to validate "manually" all the attributes for details ?

Now that you've asked that, I'm not sure.  You can try to use validates_associated and use a before_validation callback
 
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/fSjtYW_hRBEJ.

For more options, visit https://groups.google.com/groups/opt_out.
 
 

Colin Law

unread,
Jan 31, 2013, 4:10:32 AM1/31/13
to rubyonra...@googlegroups.com
On 30 January 2013 23:08, oto iashvili <optimum...@laposte.net> wrote:
> hi
>
> thanks for answer sorry I quite new in RoR. If I do how u said, will I have
> to validate "manually" all the attributes for details ?

If you are new to rails there are probably many other basic features
that you are not aware of. I suggest you work right through a good
rails tutorial such as railstutorial.org, which is free to use online.
That will introduce you to the basics. Also look through the Rails
Guides, the one called Active Record Validations and Callbacks should
cover what you are looking for. Also look through the others.

Colin
> https://groups.google.com/d/msg/rubyonrails-talk/-/fSjtYW_hRBEJ.
Reply all
Reply to author
Forward
0 new messages