sql query in views

It’s not just “not the best”, it’s a very bad idea to perform an insert from your view.

It seems like you could probably do with reading up on MVC a bit more [1]. Simply put:

* Your view should show a form requesting input, to be submitted either conventionally or using javascript

* Your controller should direct that that request and hand data to the models

* Your models should take care of storing the data

If you’ve not done so yet, you’d be well advised to spend some time with one of the many introductory books or online tutorials to see how rails is designed.

James.

1: http://en.wikipedia.org/wiki/MVC

You're creating a problem out of thin air: your view is clearly doing way more than a view should do, so don't put it all in the view. I gather you're parsing feeds of some variety; don't put that in the view either. You could easily have a FeedParser class that consumes the feeds you're interested in and stores them in the database. Your controller method would look like

@feed_objects = FeedParser.new(address_of_feed).feed_objects

your view would just been an iteration over feed_objects, and whatever database saving you want to do is all in the FeedParser class (which presumably creates instances of some ActiveRecord subclass)

Fred

The code executed in <% %> tags of an erb template is just normal ruby: you can do the same as you would elsewhere in the app (although it is a certified bad idea)

Fred

Many people have already suggested that you are not approaching this correctly. I don’t think you will find much help here while you (a) are clearly not using Rails properly and (b) have obviously not read many tutorials or any other documentation. If you had, you would (a) know that what you are trying to do is incorrect, and (b) already know how to do what you are asking, regardless of how incorrect it is. People have enough of their own work to do without entertaining the lazy.

It makes me wonder why you are even using Rails in the first place while you are intent on undermining every reason for which it actually exists.