I have an application where I have a some users, some images, and all the users can make some personal settings for each image. I designed my database like this: Users <--> Settings <---> Images After googeling on the subject i found out the HABTM not always was the best solution, I and choose to make Settings to a model. I can now find the correct Setting-object by doing something like: @setting = @session[''user''].settings.find(:first, :conditions => ["image_id = ?", params[:id]]) (where params[:id] = @image.image_id) this works great as long as the row already exists in the table. However, since i have about 10.000 images I obviously don''t want to insert 10.000 new rows every time I add a new user to the system - and thereby a specific setting might not exist. I can fix the by doing something like: @setting = @session[''user''].settings.find(:first, :conditions => ["image_id = ?", params[:id]]) if not @setting @setting = Setting.new @setting.user_id = @session[''user''].id @setting.image_id = params[:id] end but now it begins to become ugly :/ isn''t there a cleaner way of doing this? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
isn''t there a "find_or_create" function? otherwise maybe adding your own custom find_or_create method to the settings model would be a cleaner solution. Jonas wrote:> I have an application where I have a some users, some images, and all > the users can make some personal settings for each image. > > I designed my database like this: > > Users <--> Settings <---> Images > > > After googeling on the subject i found out the HABTM not always was the > best solution, I and choose to make Settings to a model. > > I can now find the correct Setting-object by doing something like: > > @setting = @session[''user''].settings.find(:first, :conditions => > ["image_id = ?", params[:id]]) > (where params[:id] = @image.image_id) > > this works great as long as the row already exists in the table. > However, since i have about 10.000 images I obviously don''t want to > insert 10.000 new rows every time I add a new user to the system - and > thereby a specific setting might not exist. > > I can fix the by doing something like: > > @setting = @session[''user''].settings.find(:first, :conditions => > ["image_id = ?", params[:id]]) > > if not @setting > @setting = Setting.new > @setting.user_id = @session[''user''].id > @setting.image_id = params[:id] > end > > but now it begins to become ugly :/ isn''t there a cleaner way of doing > this? > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
No - I could not find a find_or_create method. And i din''t understand how adding it tp the settings model is going to work. Then i need a settings object to use it, but the idea of the method is to help me find the settings object Jeremy Wells wrote:> isn''t there a "find_or_create" function? otherwise maybe adding your own > custom find_or_create method to the settings model would be a cleaner > solution.-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
you add it to the model as a class method, so you don''t need an object to call it on. Then you can call it with Setting.find_or_create(@session[''user''], params[:id]) . so in your model class you''d have something like: self.find_or_create(user_id, image_id) ret = Setting.find(:first, :conditions => ["user_id = ? and image_id = ?",user_id, image_id]) ret = Setting.create({:user_id => user_id, :image_id => image_id}) if !ret ret end Then you''d always get back a valid object. Jonas wrote:> No - I could not find a find_or_create method. And i din''t understand > how adding it tp the settings model is going to work. Then i need a > settings object to use it, but the idea of the method is to help me find > the settings object > > Jeremy Wells wrote: > >> isn''t there a "find_or_create" function? otherwise maybe adding your own >> custom find_or_create method to the settings model would be a cleaner >> solution. >> > > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---