views:

371

answers:

3

Hi,

I want users to enter a comma-delimited list of logins on the form, to be notified by email when a new comment/post is created. I don't want to store this list in the database so I would use a form_tag_helper 'text_area_tag' instead of a form helper text_field. I have an 'after_save' observer which should send an email when the comment/post is created. As far as I am aware, the after_save event only takes the model object as the argument, so how do I pass this non model backed list of logins to the observer to be passed on to the Mailer method that uses them in the cc list.

thanks

A: 

Hi,

I think the better way will be to use a tableless model. Look at Railscatsts screencast for an example. It's pretty simple.

Voldy
I do want to store the other form fields in the table. Just not this particular field with a list of logins. Do I have to create a seperate model for this?
Why not? You have the same situation as in this screencast. It's recommendation for new comment or post. You can even have polymorphic association with this model if comment and post are different models in your app (they are recommendable). Look for such screencast for details.
Voldy
Hi,I want to store all the fields on my form except one in the database. Creating a seperate model for just one field here seems like an overkill eventhough this may work. I think creating a new instance variable for the current model object with getter and setter methods is a much better solution. Thanks
Virtual attribute, as suggested Jonathan Julian, is simpler then tableless model. But in case when number models can be recommended, I think tableless model should be considered to adhere to "don't repeat yourself" principle.
Voldy
+2  A: 

You want to store the list in a virtual attribute. It will be available in the after_save callback.

Jonathan Julian
thanks, I will try this out.
thanks this works
A: 

Here are the models you would need (along w/ the form) and the virtual attribute in the user model.

# app/models/user.rb
class User < ActiveRecord::Base
 # virutal attribute and validations
 attr_accessor :unpersisted_info
 validates_presence_of :unpersisted_info
end

# app/models/user_observer.rb
class UserObserver < ActiveRecord::Observer
  def after_save(user)
    # logic here... 
  end
end

# form for view... 
<%form_for @user do |f|%>
  <%= f.text_field :unpersisted_info %>
  <%= f.submit "Go" %>
<%end%>
bseanvt
in the after_save method you have access to the "unpersisted_info" attributedef after_save(user) self.unpersisted_info.split(",").each {|item| p item}end
bseanvt