The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Controller/View logic problem

This, I believe left joins all the tables in the [] brackets. In the donations table I have a field called "deleted" with a Y or N value (I never want to delete this information for auditing purposes - just hide it from being displayed by regular users). In regular usage the statement selects the record of the person and if an donation item is marked "deleted" the @person.donations.blank? will always be true if one or more records is marked with a "deleted=Y". So using @person.donations.blank? in a View will never give me a "false" value so I can print out the first option in an if-else-end construct. Here is a bit of code from the "edit view":

So the question is how can I modify the statement "if @person.donations.blank?" to check and see if there are only deleted records or modify the controller so that a person is chosen even if all the donation records are marked as "deleted". Could I construct something like

In the wee hours I thought of this problem some more. Correct me if I am wrong but in the solution you have given what happens if there are no donation records or if there are only donation records that have been deleted. I think that no person records will be returned in those cases which of course is not the desired outcome. I think what I really should be doing is getting three record sets: one of person and residence info - this is always unique; if there is membership data then a membership recordset that I can iterate over to give a history; and, if there are donations that have the condition where deleted=N then a donation recordset. So in the controller I would have:

Sorry to be a pain but I tried to find some of the expressions you used in the API (eg. instance_eval, collect) to get a better appreciation of just what that code is doing. In the macro sence I believe I see it but could you explain just what is happening?

Sorry to be a pain but I tried to find some of the expressions you used in the API (eg. instance_eval, collect) to get a better appreciation of just what that code is doing. In the macro sence I believe I see it but could you explain just what is happening?

Rick

I'm glad you asked

You can read about it here:http://rubycentral.com/ref/ref_c_obj...#instance_eval
It basically evaluates the code given within the scope of the instance of that object, so you can define new methods, change instance variables, etc. from outside the object by using the instance of it.

What I did, which is untested but it theoretically should work, is redefine the blank? method to instead of checking whether the array is empty, it redefines the array to make all donations that are deleted to be "nil". You can read about the array#collect method here:http://rubycentral.com/ref/ref_c_array.html#collect
So the actual array is not altered, it creates a new array to return with the changes (collect! would change the actual array, but we don't want that).

Then you have an array something like [donation, nil, nil, donation, donation, nil] and the count will still give you what you started with, so you want to get rid of the nils. Ruby provides a method to do this array#compact:http://rubycentral.com/ref/ref_c_array.html#compact
After that I just ask for the length of the new array Which only contains undeleted donations.

Thanks for the explaination and yes it did indeed work as you suggested. However, one problem begets another and in an effort to gain a fuller understanding of just what I am doing I would like to prevail on you and your knowledge one more time. It seem as though I had everything working until I went to do an edit and then save the edited record. Everything seems to work but I am afraid, or at least think I have cocked-up the simplest and most elegant way of doing things. Here is my code for the controller for create, edit and update:

Now this did work whenever I did an edit but now it does not seem to save the record after edit. However, more importantly, you will see how I have identified the voter and residence tags in the partial and in the controller. I have the sneaking suspision that there is a better way of doing this but I am not sure what it is or how to express the syntax in the controller and the partial. It seems to me that everything in the partial should be prefixed with voter (as opposed to voter and residence) and then the params array plugged into the two tables through some magic. Can you guide me here?