Walter Lee Davis
2013-Jan-16 21:09 UTC
How would I model has_many and also mark one of the ''many'' as special?
I have an Image class with belongs_to :attachable, :polymorphic => true Titles can have Images, and so can People. In the Person class, I have the apposite relationship: has_many :images, :as => :attachable, :dependent => :destroy accepts_nested_attributes_for :images, :reject_if => lambda { |a| a[:file].blank? && a[:file_uid].blank? && a[:file_url].blank? && a[:name].blank? }, :allow_destroy => true This all works fine, I''ve got my forms working perfectly. Now I would like to mark one image as being the "portrait" for this person, and I''m stuck on how best to do this. I have done it in the past in what I recognize was a very hacky manner, just adding an image_id to the parent class and setting that in my controller. I''d rather go with the flow. How would you add this to the models so that the relationship can be expressed in a normal nested form? 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 https://groups.google.com/groups/opt_out.
Dave Aronson
2013-Jan-16 21:16 UTC
Re: How would I model has_many and also mark one of the ''many'' as special?
On Wed, Jan 16, 2013 at 4:09 PM, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> wrote:> I would like to mark one image as being the "portrait" for this person,I''d suggest adding that a person has_one portrait, class_name: :Image. In the UI you can have them select one of their Images (or none). -Dave -- Dave Aronson, the T. Rex of Codosaurus LLC, secret-cleared freelance software developer taking contracts in or near NoVa or remote. See information at http://www.Codosaur.us/. -- 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 https://groups.google.com/groups/opt_out.
Walter Lee Davis
2013-Jan-16 21:23 UTC
Re: How would I model has_many and also mark one of the ''many'' as special?
On Jan 16, 2013, at 4:16 PM, Dave Aronson wrote:> On Wed, Jan 16, 2013 at 4:09 PM, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> wrote: > >> I would like to mark one image as being the "portrait" for this person, > > I''d suggest adding that a person has_one portrait, class_name: :Image. > In the UI you can have them select one of their Images (or none). > > -DaveDoes that require me to add another belongs_to to the Image? If I add a has_one, won''t I have to balance that on the other side? Walter> > -- > Dave Aronson, the T. Rex of Codosaurus LLC, > secret-cleared freelance software developer > taking contracts in or near NoVa or remote. > See information at http://www.Codosaur.us/. > > -- > 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 https://groups.google.com/groups/opt_out. > >-- 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 https://groups.google.com/groups/opt_out.
Walter Lee Davis
2013-Jan-16 21:27 UTC
Re: How would I model has_many and also mark one of the ''many'' as special?
On Jan 16, 2013, at 4:09 PM, Walter Lee Davis wrote:> I have an Image class with > > belongs_to :attachable, :polymorphic => true > > Titles can have Images, and so can People. In the Person class, I have the apposite relationship: > > has_many :images, :as => :attachable, :dependent => :destroy > accepts_nested_attributes_for :images, :reject_if => lambda { |a| a[:file].blank? && a[:file_uid].blank? && a[:file_url].blank? && a[:name].blank? }, :allow_destroy => true > > This all works fine, I''ve got my forms working perfectly. Now I would like to mark one image as being the "portrait" for this person, and I''m stuck on how best to do this. I have done it in the past in what I recognize was a very hacky manner, just adding an image_id to the parent class and setting that in my controller. I''d rather go with the flow. > > How would you add this to the models so that the relationship can be expressed in a normal nested form? > > WalterI made a little dummy app to test this out in console, and got the following to work, but (maybe it''s just me) the naming seems wrong: class Person < ActiveRecord::Base attr_accessible :photo, :name has_many :photos, :as => :attachment, :dependent => :destroy belongs_to :photo end class Photo < ActiveRecord::Base attr_accessible :file_name, :name belongs_to :attachment, :polymorphic => true has_one :person end Ignore the fact that I called it Photo rather than Image in this one... Photo has_one person, but there are going to be instances where the attachment is to a Title (which doesn''t need that relationship), and I''m not sure what happens then. 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 https://groups.google.com/groups/opt_out. > >-- 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 https://groups.google.com/groups/opt_out.
Walter Lee Davis
2013-Jan-16 21:36 UTC
Re: How would I model has_many and also mark one of the ''many'' as special?
On Jan 16, 2013, at 4:23 PM, Walter Lee Davis wrote:> > On Jan 16, 2013, at 4:16 PM, Dave Aronson wrote: > >> On Wed, Jan 16, 2013 at 4:09 PM, Walter Lee Davis <waltd-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> wrote: >> >>> I would like to mark one image as being the "portrait" for this person, >> >> I''d suggest adding that a person has_one portrait, class_name: :Image. >> In the UI you can have them select one of their Images (or none). >> >> -Dave > > Does that require me to add another belongs_to to the Image? If I add a has_one, won''t I have to balance that on the other side? > > WalterJust tried this in my stunt app, and it would require me to add a person_id to the Photo / Image model, which since it''s polymorphic, is sort of beside the point. Walter> >> >> -- >> Dave Aronson, the T. Rex of Codosaurus LLC, >> secret-cleared freelance software developer >> taking contracts in or near NoVa or remote. >> See information at http://www.Codosaur.us/. >> >> -- >> 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 https://groups.google.com/groups/opt_out. >> >> > > -- > 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 https://groups.google.com/groups/opt_out. > >-- 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 https://groups.google.com/groups/opt_out.
Emily S
2013-Jan-17 16:30 UTC
Re: How would I model has_many and also mark one of the ''many'' as special?
class Person < ActiveRecord::Base attr_accessible :photo, :name has_many :photos, :as => :attachment, :dependent => :destroy # I would do this. This will save a portrait_id on a person object, which is the id of a photo. This is most efficient because if you put the "belongs_to" association on the photo object, you''ll end up with many photos with nil values for person_id. belongs_to :portrait, :class_name => "Photo" end class Photo < ActiveRecord::Base attr_accessible :file_name, :name belongs_to :attachment, :polymorphic => true end On Wednesday, January 16, 2013 4:36:53 PM UTC-5, Walter Lee Davis wrote:> > > On Jan 16, 2013, at 4:23 PM, Walter Lee Davis wrote: > > > > > On Jan 16, 2013, at 4:16 PM, Dave Aronson wrote: > > > >> On Wed, Jan 16, 2013 at 4:09 PM, Walter Lee Davis <wa...-HQgmohHLjDZWk0Htik3J/w@public.gmane.org<javascript:>> > wrote: > >> > >>> I would like to mark one image as being the "portrait" for this > person, > >> > >> I''d suggest adding that a person has_one portrait, class_name: :Image. > >> In the UI you can have them select one of their Images (or none). > >> > >> -Dave > > > > Does that require me to add another belongs_to to the Image? If I add a > has_one, won''t I have to balance that on the other side? > > > > Walter > > Just tried this in my stunt app, and it would require me to add a > person_id to the Photo / Image model, which since it''s polymorphic, is sort > of beside the point. > > Walter > > > > >> > >> -- > >> Dave Aronson, the T. Rex of Codosaurus LLC, > >> secret-cleared freelance software developer > >> taking contracts in or near NoVa or remote. > >> See information at http://www.Codosaur.us/. > >> > >> -- > >> 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...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<javascript:>. > > >> To unsubscribe from this group, send email to > rubyonrails-ta...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org <javascript:>. > >> For more options, visit https://groups.google.com/groups/opt_out. > >> > >> > > > > -- > > 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...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<javascript:>. > > > To unsubscribe from this group, send email to > rubyonrails-ta...-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org <javascript:>. > > For more options, visit https://groups.google.com/groups/opt_out. > > > > > >-- 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 To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/Due1ZdEDFoIJ. For more options, visit https://groups.google.com/groups/opt_out.
Walter Lee Davis
2013-Jan-17 16:33 UTC
Re: How would I model has_many and also mark one of the ''many'' as special?
On Jan 17, 2013, at 11:30 AM, Emily S wrote:> class Person < ActiveRecord::Base > attr_accessible :photo, :name > has_many :photos, :as => :attachment, :dependent => :destroy > > # I would do this. This will save a portrait_id on a person object, which is the id of a photo. This is most efficient because if you put the "belongs_to" association on the photo object, you''ll end up with many photos with nil values for person_id. > belongs_to :portrait, :class_name => "Photo" > end > > class Photo < ActiveRecord::Base > attr_accessible :file_name, :name > belongs_to :attachment, :polymorphic => trueThanks for the suggestion. Would I also need to add anything to the Photo class to hold up the other side of the belongs_to? has_one :person, maybe? Or would it work without that? Walter> end > > > > On Wednesday, January 16, 2013 4:36:53 PM UTC-5, Walter Lee Davis wrote: > > On Jan 16, 2013, at 4:23 PM, Walter Lee Davis wrote: > > > > > On Jan 16, 2013, at 4:16 PM, Dave Aronson wrote: > > > >> On Wed, Jan 16, 2013 at 4:09 PM, Walter Lee Davis <wa...-HQgmohHLjDZWk0Htik3J/w@public.gmane.org> wrote: > >> > >>> I would like to mark one image as being the "portrait" for this person, > >> > >> I''d suggest adding that a person has_one portrait, class_name: :Image. > >> In the UI you can have them select one of their Images (or none). > >> > >> -Dave > > > > Does that require me to add another belongs_to to the Image? If I add a has_one, won''t I have to balance that on the other side? > > > > Walter > > Just tried this in my stunt app, and it would require me to add a person_id to the Photo / Image model, which since it''s polymorphic, is sort of beside the point. > > Walter > > > > >> > >> -- > >> Dave Aronson, the T. Rex of Codosaurus LLC, > >> secret-cleared freelance software developer > >> taking contracts in or near NoVa or remote. > >> See information at http://www.Codosaur.us/. > >> > >> -- > >> 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...-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > >> To unsubscribe from this group, send email to rubyonrails-ta...-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > >> For more options, visit https://groups.google.com/groups/opt_out. > >> > >> > > > > -- > > 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...-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to rubyonrails-ta...-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit https://groups.google.com/groups/opt_out. > > > > > > > -- > 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 > To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/Due1ZdEDFoIJ. > For more options, visit https://groups.google.com/groups/opt_out. > >-- 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 https://groups.google.com/groups/opt_out.