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.
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.
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 W. 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 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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.