Using collection_select and getting an unexpected error from sqlite3 - anyone know why?

30 views
Skip to first unread message

Adam Birnbaum

unread,
May 25, 2013, 9:47:42 AM5/25/13
to rai...@googlegroups.com
Hi All,

To supplement what we are doing in class, I am trying to develop a simple web tool to keep track of people who owe you money.  I had the app working, but have recently introduced an error message and hit a wall using collection_select, which allows you to dynamically populate a drop-down list.  Anyone have any experience using this method?  I created a post on stackoverflow describing the issue: 

http://stackoverflow.com/questions/16743217/using-collection-select-and-getting-an-unexpected-error-from-sqlite3-anyone-kn

Incidentally, this is my first post to stackoverflow - anyone have any tips/tricks on how to get your questions answered by the community?  I already tried to research to see if my question had already been asked/answered, provide all relevant details in my post, and organize the content logically. If you think the question is well done and deserves to be upvoted I would appreciate that as well.

Thanks,
Adam



Michael Maddox

unread,
May 25, 2013, 10:28:12 AM5/25/13
to Adam Birnbaum, rai...@googlegroups.com
Adam,

I'm not a rails expert, but I can give you some tips on StackOverflow.

Your question is long.  Super long.  How could you make it shorter?  Much shorter?

It's great that you've included all that code.  But which code could be removed and still have the issue easily reproducible for someone reading the question?  Sometimes making a super small test case to reproduce a problem really clarifies what the issue is for me.  Sometimes you end up with a similar implementation that doesn't have the error and you can work the two solutions towards each other until you discover what the meaningful difference is.

Since you are getting a SQL error, yet there is no SQL in your code, Rails is doing magic for you and your best bet is maybe to somehow turn on SQL tracing so you can see what SQL Rails is generating behind the scenes.  That may be useful to add to your question.

Good luck!

-Michael






--
You received this message because you are subscribed to the Google Groups "RailsMN" group.
To unsubscribe from this group and stop receiving emails from it, send an email to railsmn+u...@googlegroups.com.
To post to this group, send email to rai...@googlegroups.com.
Visit this group at http://groups.google.com/group/railsmn?hl=en.
 
 

Joshua Wehner

unread,
May 25, 2013, 5:38:07 PM5/25/13
to Adam Birnbaum, rai...@googlegroups.com
I don't have a lot of experience with StackOverflow, but I'll try to help out here, anyway.

There's nothing obviously wrong with the code here, and trying out a new Rails app locally with this Payments model, I can't replicate the error you're getting. But there are a few unusual choices you're making here that I'm suspicious of, and any time you're having a hard-to-undesrtand error, it's helpful to try to narrow down the list of things that could be responsible. 

Here are a couple of things I would want to straighten out before troubleshooting further:

* the first line of Payment requires "valid_email", but you don't say where that comes from, and since I don't have "valid_email", the require fails for me. it's possible (but unlikely) that it's adding something unexpected, since commenting that line out does make the Payment class work as expected for me. (It's also very likely that whatever "valid_email" does, it's not really necessary -- see http://davidcel.is/blog/2012/09/06/stop-validating-email-addresses-with-regex/ for more.)

* including ActiveModel::Validations is unnecessary here, since that module is already included into ActiveRecord, which Payments has as it's parent. I don't *think* it's causing the error you are seeing, but including it twice could have hard-to-predict consequences.

* current_user.payments.new isn't really what you want -- try current_user.payments.build instead. (As a general rule: you use "new" with upper-case classes, like Payment.new; use "build" with lower-case "associations", like user.payments.) You can read more about associations and "build" here: http://guides.rubyonrails.org/association_basics.html

@payment_current_user = current_user.payments.all is not likely to be causing your error, but it's not really the best way to get the list of user payments. For one thing, it seems it's only used in the "new" view, but you're calling it in the "create" action because when "if save" fails, you wind up rendering the new action. If the user has many payments, this line will load all of them during the "create" action, but if the save is successful, the user will be redirected away and none of these payments would be needed -- meaning that you've exercised your database for no particularly good reason.

It's also not really necessary -- replace any instances of @payment_current_user in your view with "current_user.payments" and you'll be fine. As a general rule, it's best if your controller only instantiates at most one "at-variable", focus on the data you need in the create action (the new payment) and let the view get the data it needs from @payment and current_user. (The exception(s) to "let the view get the data": at most one ".whatever" and don't call ".find" or ".where" from a view.)


One thing: what I'm doing here (adding your Payment model to a brand-new Rails app locally) is close, but not really replicating your setup over there -- you might have before_filters in your controllers or gems or plugins that add additional filters to ActiveRecord or who knows what lurking in your application that I can't predict. If you can't make your whole app available as a public repository on GitHub, you could at least post the complete controller in a gist (https://gist.github.com).

If nothing else, putting the complete stack trace in a gist gives us some more information about the "layers" of code interacting to produce the error you see.



Adam Birnbaum

unread,
May 26, 2013, 9:46:58 AM5/26/13
to Joshua Wehner, rai...@googlegroups.com
Thanks Mike and Joshua for your very helpful comments.  I edited my post to streamline the code provided and organize the content better, and will keep in mind for next time the idea of keeping posts short.

I made all of the changes you suggested, Joshua, to my code -- it was helpful to learn by doing and get a better understanding of best practices I'll know in the future.  Unfortunately, none of these changes fixed my issue, but I did end up figuring out what was wrong late last night.  The solution is posted at the original Stackoverflow link if you are interested.

Thanks again to both of you for your help!

Adam

Reply all
Reply to author
Forward
0 new messages