Sql query in views

Hello frnds

Is there any method to write the insert query in the .rhtml file (eg:
-list.rhtml). If so please do let me know. I know that its not the best
way out but then in my list.rhtml i m having @email which is storing
email addresses so i
want to store those into my db how could i do that?

please let me know…

thanks

On 4 Oct 2007, at 14:19, Dhaval P. wrote:

Is there any method to write the insert query in the .rhtml file (eg:
-list.rhtml). If so please do let me know. I know that its not the
best
way out but then in my list.rhtml i m having @email which is storing
email addresses so i
want to store those into my db how could i do that?

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: MVC - Wikipedia


James S.
Play: http://james.anthropiccollective.org
Work: Processing Greenbelt 2009 – James Stewart

yes what you said is perfectly correct I agree to it…but the thing is
that I dont have any form i m just generating the database using feeds
and then i need to filter the data and that filtered data should go into
the other table…as soon as it is fetched…is there any way out for it?
Pls reply

thanks a lot

dhaval parikh

Dhaval P. wrote:

yes what you said is perfectly correct I agree to it…but the thing is
that I dont have any form i m just generating the database using feeds
[…]

Do I understand correctly that you’re parsing incoming data from feeds
and storing it into the DB?

If so, then here’s one idea (I’m new to Rails, though not to MVC, so
please be aware that this may not be the most “Rails-like” solution):

  • View queries the feed for new data.
  • Controller parses the data.
  • Model inserts parsed data into DB.

I can’t think of any reason why you’d ever need an insert in a view in
any MVC architecture.

Best,
Marnen Laibow-Koser

ya perfect…can u tell me how can i manage it thru model…

Dhaval P. wrote:

ya perfect…can u tell me how can i manage it thru model…

The same way you’d insert a record from a form. The model doesn’t care
where the data comes from. Remember, part of the point of MVC is the
separation of concerns.

Best,
Marnen Laibow-Koser

Marnen Laibow-Koser wrote:

Dhaval P. wrote:

ya perfect…can u tell me how can i manage it thru model…

The same way you’d insert a record from a form. The model doesn’t care
where the data comes from. Remember, part of the point of MVC is the
separation of concerns.

Best,
Marnen Laibow-Koser

if you can give me some example or link for ref it would be gr8…

thanks

I would explain the question in a better way…the thing is that in view
i m diaplying the fetched data and storing that in a variable…

@email
@desc
@sub
@body

these r the 4 variables which i m getting…now the value of this
variable i want to store it into the database…so what to do…remember
tht this variables are declered in views…so just after the completion
of loop i want it to get inserted into the database…what should i do
for the same…

Thanks

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

well wat ur saying i can understand but i just dont want to do
that…even if its a wrong method i just want to know if i can insert
values in the database after fetching it thru views…pls also give
example or query i mean how to write that…

On 9 Oct 2007, at 10:12, Dhaval P. wrote:

well wat ur saying i can understand but i just dont want to do
that…even if its a wrong method i just want to know if i can insert
values in the database after fetching it thru views…pls also give
example or query i mean how to write that…

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

well i m doing the same but its not working …can u tell me say suppose
i want to insert @email which is fetched to the db what query should i
write.there. direct insert into table doesnt work

On 10/9/07, Dhaval P. [email protected] wrote:

well i m doing the same but its not working …can u tell me say suppose
i want to insert @email which is fetched to the db what query should i
write.there. direct insert into table doesnt work

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.