Trying to make a user list from 2 fields

My model has two fields that I want in a customer list for an index
page. They are name and second_name. I need a way to combine them in a
collection with their id numbers so the Edit links will work. Sounds
like SQL, but I’m not sure.

maybe something like:
SELECT name, id FROM households and SELECT second_name, id FROM
households

just not sure how to make ONE statement from these two including
both…

Thanks

Bob

Bob S. wrote:

My model has two fields that I want in a customer list for an index
page. They are name and second_name. I need a way to combine them in a
collection with their id numbers so the Edit links will work. Sounds
like SQL, but I’m not sure.

maybe something like:
SELECT name, id FROM households and SELECT second_name, id FROM
households

just not sure how to make ONE statement from these two including
both…

Thanks

Bob

I’m not clear what you exactly want…

If you want to display name, second name in ur page,

use this in controller,

Assuming ur model name is Household

@households = Household.all

& in the view,

@households.each do |hh|
hh.name + hh.second_name
end

this is wht u want?

regards,
Srikanth
http://srikanthjeeva.blogspot.com

On 20 August 2010 06:41, Bob S. [email protected] wrote:

My model has two fields that I want in a customer list for an index
page. They are name and second_name. I need a way to combine them in a
collection with their id numbers so the Edit links will work. Sounds
like SQL, but I’m not sure.

I don’t understand what you mean by ‘their id numbers’. The household
record will have an id but the individual fields do not, they just
have the text of the name and second_name. Perhaps you are talking
about relationships to other models rather than fields in the model?
If so then we need to know the relationships.

Colin

All I need is a list of names and their id field so I can setup links
to edit them from the view. The problem is that there are two name
lists. I need to combine them and have either rails or SQL order them
alphabetically.

What I need returned is a list of the names in the fields name and
sname put together in alphabetical order with their ids
so I can make this list into an index screen with edit links. I assume
the id would be Household.id, just looking for a way to combine
two name fields for the index page.

            Bob

On 21 August 2010 08:40, Bob S. [email protected] wrote:

All I need is a list of names and their id field so I can setup links
to edit them from the view. The problem is that there are two name
lists. I need to combine them and have either rails or SQL order them
alphabetically.

Please don’t top post, it makes it difficult to follow the thread.
What do you mean a list of names and their id fields? You said that
the names are fields in your model, so they do not have id’s of their
own. I asked that question last time and you have not answered it.

Colin

On Aug 23, 3:24 am, Colin L. [email protected] wrote:

<%= “#{household.name1} #{household.name2}” %>

           Bob

What do you mean a list of names and their id fields? You said that

collection with their id numbers so the Edit links will work. Sounds
maybe something like:


You received this message because you are subscribed to the Google G. “Ruby on Rails: Talk” group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.

I have 2 name fields called name and sname. What I am trying to do is
show both names in the index page at the beginning of
the app. They need to be combined as a list which is then put in
alphabetical order and given to the index page to be displayed with
edit links for each item.

Bob

On 22 August 2010 23:04, Bob S. [email protected] wrote:

Once again, please do not top post. Insert your reply a appropriate
points in the previous message. Thanks

What I need returned is a list of the names in the fields name and
sname put together in alphabetical order with their ids
so I can make this list into an index screen with edit links. I assume
the id would be Household.id, just looking for a way to combine
two name fields for the index page.

I think I am slowly managing to get some grasp of what you are trying
to achieve, though it is proving very difficult to get information
from you.

Is it right that you want a view showing a list of households where
for each household you show the name which is a concatenation of two
fields from the household object? If so then you can do something
like

<% @households.each do |household| %>
<%= “#{household.name1} #{household.name2}” %>
<% end %>

However it would be better to provide a method oh Household that
returns the combined name so you can use
<%= household.combined_name %>

Colin

On Aug 23, 4:06 pm, Colin L. [email protected] wrote:

So which part is giving the problem? Fetching the records from the
database in the right order (in the controller), making the records
available to the view, showing the names on the view or showing the
edit links?

[This would be a good place to insert your reply]

Colin

Bob, what Colin (and others) are trying to understand is what you’re
actually trying to do. When you say that you have 2 name fileds (name
and sname) do you mean that you have a first name and a surname, each
in a Household record (i.e., corresponding, for example, to the head
of the household), or do you mean that you have another model (i.e.,
Person) in which you have fields for name, sname, and household_id
(thereby relating the Person record to the Household to which the
person belongs, with associated relationships in the models)?

As you indicated that you want to display the id of the “name or
sname” the inference which I draw is that they will be different,
thereby implying that your two name fields are in a model different
from the Household model (as they would otherwise both have the same
id, corresponding to the id of the Household record). If both name
and sname are fields in the same (Person) record (and related to a
particular Household record), then concatination can be done in your
view, model, or a helper, with the better practice being in the model
or helper. Note that you probably do not want hh.name + hh.sname (as
suggested above) as that would look strange. You probably would want
either hh.sname + ', ’ + hh.name or hh.name + ’ ’ + hh.sname instead.

Until you ask a clearly defined question, it is impossible to guess
what you want or to provide you with the appropriate guidance.

Sandy

Bob S. wrote:

I have 2 name fields called name and sname. What I am trying to do is
show both names in the index page at the beginning of
the app. They need to be combined as a list which is then put in
alphabetical order and given to the index page to be displayed with
edit links for each item.

Bob

controller:

def index

get ordered households

@households = Household.find(:all, :order => ‘sname, name’)
end

index view:

<% @households.each do |hh| %> <% end %>
<%= link_to 'Edit Me!', edit_household_path(hh) %> <%= link_to 'Old school link', :controller => 'households', :action => 'edit', :id => hh.id %> <%= h(hh.sname)+', '+h(hh.name) %>

or if you simply want a list of links:

<% @households.each do |hh| %> <% end %>
<%= link_to h(hh.sname)+', '+h(hh.name), edit_household_path(hh) %>

or something like that…

On Aug 24, 4:14 pm, Ar Chron [email protected] wrote:

controller:

<%= link_to ‘Edit Me!’, edit_household_path(hh) %>
<% @households.each do |hh| %>

<%= link_to h(hh.sname)+’, '+h(hh.name),
edit_household_path(hh) %>

<% end %>

or something like that…

Posted viahttp://www.ruby-forum.com/.

The problem I’m having is in combining these two name lists into one
list, then putting it in alphabetical order.
Name is for head of household, and sname is for a second person,
either a wife or husband. These are two fields in the same record.
Combining the lists and putting them in alphabetical order will show a
full list to match whoever comes for the food. This program is for a
local foodbank.

Thanks
Bob

On Aug 25, 7:53 am, Bob S. [email protected] wrote:

The problem I’m having is in combining these two name lists into one
list, then putting it in alphabetical order.
Name is for head of household, and sname is for a second person,
either a wife or husband. These are two fields in the same record.
Combining the lists and putting them in alphabetical order will show a
full list to match whoever comes for the food. This program is for a
local foodbank.

Sounds like Ar Chron’s code would do it. Did you try his suggestion ?

Fred

On 25 August 2010 08:02, Frederick C. [email protected]
wrote:

full list to match whoever comes for the food. This program is for a
local foodbank.

Sounds like Ar Chron’s code would do it. Did you try his suggestion ?

I think I have just realised what the OP is after, he wants each
record to appear twice in the list, once showing name and once showing
sname with the two record sets interleaved so that the displayed names
are in alpha order.
OP is that correct?

Colin

On 23 August 2010 20:20, Bob S. [email protected] wrote:
When I suggested not top posting, I suggested inserting your post in
the previous post in order that it appears after the question it is
answering. Not just sticking the reply down the bottom.

I have 2 name fields called name and sname. What I am trying to do is
show both names in the index page at the beginning of
the app. They need to be combined as a list which is then put in
alphabetical order and given to the index page to be displayed with
edit links for each item.

So which part is giving the problem? Fetching the records from the
database in the right order (in the controller), making the records
available to the view, showing the names on the view or showing the
edit links?

[This would be a good place to insert your reply]

Colin

On Aug 25, 6:04 am, Sandy [email protected] wrote:

Name is for head of household, and sname is for a second person,
are in alpha order.
scope to using a “flat file” which always includes two People per
Given the foregoing (and without adding a Person model and
2 Smith, Sam Doe, Jill
Doe, Joan 1
@households = Household.all
end

<th>Sname</th>

sure?’, :method => :delete %>

Name Household ID

Doe, Jane 4
and doing so would allow for one or more “recipients” per household.
On the other hand, the forms used to create households and authorized
recipients would be more complex, or maybe you’re dealing with a
legacy database having the flat file structure.

Now… is this what you wanted?

Sandy

In the interest of completely responding to your question about
getting the “Edit” links, and to simplify the view code, you may do
the sorting in the household_controller. Then, you would simply
iterate over the (already) sorted @sorted array. The only other
“trick” is that you do have to use the find function in the view, as
the restful routing is based on the household object.

Finally, in my penultimate paragraph, I meant Person (not People)
model.

That being said, here is the revised code:

Controller:

def index
@households = Household.all

@sorted = Array.new
for n in @households do
  @sorted << { :aname => n.name, :id => n.id }
  @sorted << { :aname => n.sname, :id => n.id }
end
@sorted = @sorted.sort_by { |n| n[:aname] }

respond_to do |format|
  format.html # index.html.erb
  format.xml  { render :xml => @households }
end

end

View:

Listing Households

<% @households.each do |household| %>

<% end %>
id Name Sname
<%=h household.id %> <%=h household.name %> <%=h household.sname %> <%= link_to 'Show', household %> <%= link_to 'Edit', edit_household_path(household) %> <%= link_to 'Destroy', household, :confirm => 'Are you sure?', :method => :delete %>

Recipients

<% @sorted.each do |n| %> <% end %>
Name Household ID
<%= n[:aname] %> <%= n[:id] %> <%= link_to 'Show', Household.find(n[:id]) %> <%= link_to 'Edit', edit_household_path(Household.find(n[:id])) %> <%= link_to 'Destroy', Household.find(n[:id]), :confirm => 'Are you sure?', :method => :delete %>

<%= link_to ‘New household’, new_household_path %>

Now, the Show, Edit, and Destroy links are shown with each Recipient
in the list of Recipients, but they are routed to the associated Show,
Edit, and Destroy functions in the household_controller. Again, I have
shown the id from the households table in both the Households Listing
and the Recipients listing so that it’s easier to see what’s going on.
You will probably need only the Recipients listing portion of
index.html.erb, but the way I coded it, you can easily see that it
works. As always, I test code before posting it here.

Sandy

On Aug 25, 4:00 am, Colin L. [email protected] wrote:

Combining the lists and putting them in alphabetical order will show a

Colin

Ok, Bob,

The problem now seems to be that there is a Household model which
contains two fields, name and sname, each of which is the full name
(hopefully in the format of last_name, first_name) of a Person in the
Household (although it now appears that the question is limited in
scope to using a “flat file” which always includes two People per
Household). While I would suggest using a second, Person, model,
whereby each Household could include one or more “authorized” People,
that does not appear to be the question.

Assuming that I now understand the Household model, the question
appears to be, “How do I create a view which interleaves the names
(name and sname) from multiple Household records, alphabetically, with
two columns per row (“aname” and “id”) from the households table,
where “aname” is either “name” or “sname” from the original table.”

Given the foregoing (and without adding a Person model and
relationships), the way I would implement the controller would be to
create a new array of hashes of the form {:aname, :id}, then add an
element to the array from name/id and sname/id hash for each row in
the households table. Then, in the view, I would simply sort the new
array by aname, and display the sorted array.

Assuming the following Household records:

id name sname
1 Doe, John Doe, Joan
2 Smith, Sam Doe, Jill
3 Adams, Art Little, Louise
4 Jones, Tom Doe, Jane

The sorted array would display, as follows:

Recipients
Name Household ID
Adams, Art 3
Doe, Jane 4
Doe, Jill 2
Doe, Joan 1
Doe, John 1
Jones, Tom 4
Little, Louise 3
Smith, Sam 2

For simplicity (without dealing with routes.rb), you can provide the
following code in the households_controller to show the above in the
view (index.html.erb):

def index
@households = Household.all

@sorted = Array.new
for n in @households do
  @sorted << { :aname => n.name, :id => n.id }
  @sorted << { :aname => n.sname, :id => n.id }
end

respond_to do |format|
  format.html # index.html.erb
  format.xml  { render :xml => @households }
end

end

Then, in the view, index.html.erb:

Listing Households

<% @households.each do |household| %>

<% end %>
id Name Sname
<%=h household.id %> <%=h household.name %> <%=h household.sname %> <%= link_to 'Show', household %> <%= link_to 'Edit', edit_household_path(household) %> <%= link_to 'Destroy', household, :confirm => 'Are you sure?', :method => :delete %>

Listing Recipients

<% @sorted.sort_by { |n| n[:aname] }.each do |n| %> <% end %>
Name Household ID
<%= n[:aname] %> <%= n[:id] %>
<%= link_to 'New household', new_household_path %>

Thereby, providing the following output in the view:

Listing Households
id Name Sname
1 Doe, John Doe, Joan Show Edit Destroy
2 Smith, Sam Doe, Jill Show Edit Destroy
3 Adams, Art Little, Louise Show Edit Destroy
4 Jones, Tom Doe, Jane Show Edit Destroy

Listing Recipients
Name Household ID
Adams, Art 3
Doe, Jane 4
Doe, Jill 2
Doe, Joan 1
Doe, John 1
Jones, Tom 4
Little, Louise 3
Smith, Sam 2
New household

Of course, it would be trivial to create the desired view if the
application included a People model, related to the Household model,
and doing so would allow for one or more “recipients” per household.
On the other hand, the forms used to create households and authorized
recipients would be more complex, or maybe you’re dealing with a
legacy database having the flat file structure.

Now… is this what you wanted?

Sandy

On Aug 25, 10:39 am, Ar Chron [email protected] wrote:

Yes, that would change the landscape a bit. Sounds like Sandy’s
solution has the right of it by using @households to build @sorted,
thereby replicating the household record for each resident.


Posted viahttp://www.ruby-forum.com/.

I think that was the issue that Bob was trying to address, and I’m
hoping it was due to his having inherited some legacy flat file
database, and possilby not knowing how to normalize it, or being
prevented from doing so by something else in the application which
uses the households table. Otherwise, the application cries out for
relationships between a Person model (a “belongs_to :household”
relationship) and the Household model (a “has_many :people”). While
this does complicate, slightly, the addition of people to households,
it would be a better way to approach the problem, and it would allow
for more than two “authorized receivers” per household (although I
have no idea of what the business rules are for his application, and
they could be… 2 and only 2 per household).

Next, @sorted, as defined in the households_controller, does not
replicate the household record for each “authorized receiver” (I
prefer that term, rather than “resident”, because a household could
include many residents, only two of whom are authorized to receive
food from the food bank). What @sorted does is to provide an array
sorted by the names of the “authorized users” with the id of the
relevant household record. With that id, one can use Household.find
to obtain the relevant household record and then obtain any other
field from that record. Of course, if any additional fields were
needed in the view, they could be added to the hash in the @sorted
array when it was created in the controller, thereby avoiding the need
to do the lookups in the view and keeping the data access logic out of
the view.

While I did use @households in the households_controller, the only
reason I did that was so that I could display both households and
receivers in the same index.html.erb view, so that it was clear that
the @sorted array was providing the “authorized users” in their sorted
order with the id of the relevant household record. Otherwise, better
practice would be to have households be a local variable within the
controller.

Sandy

Sandy wrote:

On Aug 25, 4:00�am, Colin L. [email protected] wrote:

Combining the lists and putting them in alphabetical order will show a

Colin

Ok, Bob,

The problem now seems to be that there is a Household model which
contains two fields, name and sname, each of which is the full name

Yes, that would change the landscape a bit. Sounds like Sandy’s
solution has the right of it by using @households to build @sorted,
thereby replicating the household record for each resident.

Sandy wrote:

On Aug 25, 10:39�am, Ar Chron [email protected] wrote:

Yes, that would change the landscape a bit. �Sounds like Sandy’s
solution has the right of it by using @households to build @sorted,
thereby replicating the household record for each resident.


Posted viahttp://www.ruby-forum.com/.

I think that was the issue that Bob was trying to address, and I’m
hoping it was due to his having inherited some legacy flat file
database, and possilby not knowing how to normalize it, or being
prevented from doing so by something else in the application which
uses the households table. Otherwise, the application cries out for
relationships between a Person model (a “belongs_to :household”
relationship) and the Household model (a “has_many :people”). While
this does complicate, slightly, the addition of people to households,
it would be a better way to approach the problem, and it would allow
for more than two “authorized receivers” per household (although I
have no idea of what the business rules are for his application, and
they could be… 2 and only 2 per household).

Next, @sorted, as defined in the households_controller, does not
replicate the household record for each “authorized receiver” (I
prefer that term, rather than “resident”, because a household could
include many residents, only two of whom are authorized to receive
food from the food bank). What @sorted does is to provide an array
sorted by the names of the “authorized users” with the id of the
relevant household record. With that id, one can use Household.find
to obtain the relevant household record and then obtain any other
field from that record. Of course, if any additional fields were
needed in the view, they could be added to the hash in the @sorted
array when it was created in the controller, thereby avoiding the need
to do the lookups in the view and keeping the data access logic out of
the view.

While I did use @households in the households_controller, the only
reason I did that was so that I could display both households and
receivers in the same index.html.erb view, so that it was clear that
the @sorted array was providing the “authorized users” in their sorted
order with the id of the relevant household record. Otherwise, better
practice would be to have households be a local variable within the
controller.

Sandy

Many thanks again Sandy. Another problem has appeared. The list is
created and displayed correctly, but won’t let the search function
narrow down the list. This works perfectly using the Household variable,
but won’t narrow the search using the array created by adding both names
together. The way I have it, the person entering the information only
needs to get a persons license and enter the first few letters of the
last name, narrowing the list down to a few entries that match and
clicking the edit link next to the name wanted.

This search function, paginate, and the use of an array other than the
Household variable don’t seem to work together. I included the parts of
controller and view that have this problem…

household_controller.rb
def make_double_list
@households = Household.all
$households = Array.new
@sorted = Array.new

debugger

for n in @households do
 @sorted << { :last_name => n.last_name, :first_name => 

n.first_name, :address => n.address, :id => n.id }
if !n.slast_name.blank? then @sorted << { :last_name =>
n.slast_name, :first_name => n.sfirst_name, :address => n.address, :id
=> n.id } end
end
$households = @sorted.sort_by { |x| x[:last_name] }
end

def index
make_double_list
if $hidden.blank? then $hidden = 0 end
#debugger
conditions = ["last_name like ? ", “#{params[:search]}%”]
if $hidden == 0 then
@households = Household.paginate :per_page => 16,
:page => params[:page],
:order => ‘last_name’,
:conditions => conditions
else
@households = ArchivedHousehold.paginate :per_page => 16,
:page => params[:page],
:order => ‘last_name’,
:conditions => conditions
$hidden = 0
end
end

def search

debugger

conditions = ["last_name like ?", "#{params[:search]}%"]
if $hidden == 0 then
  @households = Household.paginate :per_page => 16,
                                   :page => params[:page],
                                   :order => 'last_name',
                                   :conditions => conditions
  debugger
else
  @households = ArchivedHousehold.paginate :per_page => 16,
                                   :page => params[:page],
                                   :order => 'last_name',
                                   :conditions => conditions
end
render :partial=>"search", :layout=>false

end

index.html.erb

Index of Households

<%= text_field_tag 'search', params['search'], :autocomplete => "off" %> <%= observe_field 'search', :frequency => 0.5, :update => 'searchdiv', :url => {:controller => 'households', :action => 'search'}, :with => "'search=' + encodeURIComponent(value) " %>
<%= render :partial=>"search" %>
<% form_tag new_household_path do%>
  <%= if $hidden == 0 then submit_tag "New household" end %>
<% end %>
<%= will_paginate @households %>

_search.html.erb

<% for n in @households %> <% if $hidden == 0 %> <% end %> <% if $hidden == 1 %> <% end %> <% if $hidden == 0 %> <% end %> <% end %>
Last Name First Name Address
<%= link_to 'Edit', :action => 'edit', :id => n[:id] %><%= link_to 'Restore', :action => 'restore', :id => n[:id] %><%= n[:last_name] %> <%= n[:first_name] %> <%= n[:address] %> <%= link_to 'Delete', :action => 'delete', :id => n[:id], :confirm => 'Are you sure ?' %>

On Aug 25, 5:17 pm, Sandy [email protected] wrote:

Ok, Bob,

have no idea of what the business rules are for his application, and
field from that record. Of course, if any additional fields were
practice would be to have households be a local variable within the
controller.

Sandy

Thanks for the replies, Sandy. That was very close to what I needed…
The problem now is that I am using a text field with observe_field to
allow the list to be shrunk, as the beginning of the name is entered.
Then the household list is displayed with will_paginate. I can’t seem
to fit the conditions to reduce the list and a call to paginate into
the code you provided. Below is my controller and view code.

By the way, the household class represents a household (novel, eh?).
It has HOH and spouse names, street address, mailing, zip, phone, and
income info.
There is a person model that only holds sex and birthday of each
person in the house. This is used for lists for christmas and to
report what age people and families used the foodbank.

class HouseholdsController < ApplicationController
helper ‘households’

def admin
@date = Time.now
end

def back
$hidden = 0
redirect_to(households_url)
end

def backup
result = ~/foodshelf/script/backup
flash[:notice] = ‘Backup completed’
redirect_to(:action => ‘index’)

debugger

end

def christmas
# debugger
# children under 12
@birthday = Date.today-12.years
@f = Household.find(:all, :conditions => ["people.birthday

:bday", {:bday => @birthday}], :joins => [:people])
@family12 = @f.uniq.sort_by {|r| r.name_list}
@test = Household.count :all, :group =>
‘people.household_id’, :joins => :people
respond_to do |format|
# format.xml { head :ok }
format.pdf { send_data render_to_pdf( :action => ‘christmas’,
:layout => ‘pdf_report’),
:filename =>
“Christmas_report_for_” + Date.today.strftime(’%Y’) }
end
end

def copy
result = ~/foodshelf/script/copy
flash[:notice] = ‘Copy completed’
redirect_to(:action => ‘index’)
end

def create
#debugger
@household = Household.new()
@churches = Church.find(:all, :order => “name”).map {|u| [u.name, u.id]}
#debugger
fix_dates
if @household.update_attributes(params[:household])
@v =
Visit.find_or_create_by_household_id_and_month_and_year(:household_id
=> params[:id], :month => @month, :year => @year)
@v.update_attributes(params[‘visit’])
flash[:notice] = ‘Household was successfully saved.’
redirect_to households_path
else
#debugger
flash[:notice] = ‘Create failed - look for a red field name’
render(:action => :new)
end
end

DELETE /households/1

DELETE /households/1.xml

def destroy
# debugger
@household = Household.find(params[:id])
flash[:notice] = ‘Household was deleted.’
@household.destroy

@delcmd = 'delete from visits where household_id = ’ +

@household.id.to_s

debugger

Visit.connection.execute(@delcmd)

#  flash[:notice] = "Successfully destroyed household."
redirect_to(households_url)

end

def daysinmonth(mon)
(Date.new(Time.now.year,12,31).to_date<<(12-mon)).day
end

GET /households/1/edit

def edit

debugger

@today = Date.today
@household = Household.find(params[:id])
@churches = Church.find(:all, :order => "name").map {|u| [u.name,

u.id]}
# debugger
fix_dates
@v =
Visit.find_or_create_by_household_id_and_month_and_year(:household_id
=> params[:id], :month => @month, :year => @year)
@monthly = @v.monthly

debugger

end

def fix_dates
# Get week of next Thursday
# week is week of month
@today = Date.today
@day = @today.strftime(’%d’).to_i
if (@day>8)
@thisthurs = Chronic.parse(‘last thursday’)
else
@thisthurs = Chronic.parse(‘next thursday’)
end
@dayofthisthurs = @thisthurs.strftime(’%d’).to_i
@monthofthisthurs = @thisthurs.strftime(’%m’).to_i
@yearofthisthurs = @thisthurs.strftime(’%Y’).to_i
@month = Date.today.mon()
@daysinmonth = daysinmonth(@month)
@year = @yearofthisthurs
@dayoffirstthurs = @dayofthisthurs
while @dayoffirstthurs>7
@dayoffirstthurs -= 7
end
#debugger
if (@day <= @dayoffirstthurs) # for getting correct week for fruit/
bread
@week = 1
@monthname = @today.strftime(’%B’)
elsif (@day <= @dayoffirstthurs+7)
@week = 2
@monthname = @today.strftime(’%B’)
elsif (@day <= @dayoffirstthurs+14)
@week = 3
@monthname = @today.strftime(’%B’)
elsif (@day <= @dayoffirstthurs+21)
@week = 4
@monthname = @today.strftime(’%B’)
elsif (@day <= @dayoffirstthurs+28 && @dayoffirstthurs+28 <=
daysinmonth(@month))
@week = 5
@monthname = @today.strftime(’%B’)
else
@week = 1
@nextmonth = @today>>1
@monthname = @nextmonth.strftime(’%B’)
end
end

def get_person
session[:person] ||= Person.new
end

def hidden
$hidden = 1
redirect_to(households_url)
end

def idreport
@today = Date.today

@test2= Household.find(:all)

@test = @test2.sort_by{|c| [c.name_list, c.second_name_list] }

@idstring = sprintf("select households.id, households.last_name,

households.first_name from households order by last_name, first_name")
# debugger
@id = Household.find_by_sql(@idstring)
respond_to do |format|
# format.xml { head :ok }
format.pdf { send_data render_to_pdf( :action => ‘idreport’,
:layout => ‘pdf_report’),
:filename =>
“ID_List_for_” + @today.strftime(’%m%d%%Y’) }
end
end

GET /households

GET /households.xml

def index
conditions = ["last_name like ? ", “#{params[:search]}%”]
if $hidden == 1 then
@households = ArchivedHousehold.paginate :per_page => 16,
:page => params[:page],
:order => ‘last_name’,
:conditions => conditions
else
@households = Household.paginate :per_page => 16,
:page => params[:page],
:order => ‘last_name’,
:conditions => conditions
$hidden = 0
end
end

def monthly
#debugger
@start = Date.new(params[:year].to_i, params[:month].to_i, 1)
@end = @start>>1
@newstring = sprintf(“select last_name, first_name from households
where start_date >= ‘%10s’ and start_date < ‘%10s’ order by last_name,
first_name;”,@start,@end)

debugger

@new = Household.find_by_sql(@newstring)
@repeatstring = sprintf("select households.last_name,

households.first_name from households,visits where " +
"households.id=visits.household_id and
visits.month=’%2d’ and visits.year = ‘%4d’ " +
"and visits.monthly = 1 and (households.start_date

= ‘%10s’ or households.start_date < ‘%10s’) " +
“order by
households.last_name,households.first_name;”, @start.month,
@start.year, @end, @start)
@repeat = Household.find_by_sql(@repeatstring)
@churches = Church.find(:all, :order => “name”).map {|u| [u.name, u.id]}

respond_to do |format|
  # format.xml { head :ok }
  format.pdf { send_data render_to_pdf( :action => 'monthly',
                                        :layout => 'pdf_report'),
                                        :filename =>

“Monthly_report_for_” + @start.strftime(’%B’) + “_” +
@start.year.to_s }
end
end

GET /households/new

GET /households/new.xml

def new
@today = Date.today
@lastthurs = Chronic.parse(‘last thursday of month’)
@dayoflastthurs = @lastthurs.strftime(’%d’).to_i
@household = Household.new
@household.people.build
fix_dates
end

def print

debugger

@churches = Church.find(:all, :order => "name").map {|u| [u.name,

u.id]}

@households = Household.find(:all, :order => "last_name,

first_name")
@households = Household.find(278,279)
# encrypt ??
# if not encrypted, delete ?
date=date +%Y%m%d-%H:%M
respond_to do |format|
format.pdf { send_data render_to_pdf( :action => ‘print’,
:layout => ‘pdf_report’),
:filename =>
“Household_List_for_” + date }
end
end

def putadmin
debugger
end

def reset_links
Household.find(:all).each { |h|
if h.sfirst_name.nil? then
h.sfirst_name=’ ’
end
if h.smiddle.nil? then
h.smiddle=’ ’
end
if h.slast_name.nil? then
h.slast_name=’ ’
end
h.save
}
redirect_to(households_url)
end

def restore
Household.restore_all([ ‘id = ?’, params[:id] ])
flash[:notice] = ‘Household was restored.’
$hidden = 0
redirect_to(households_url)
end

def search
conditions = [“last_name like ?”, “#{params[:search]}%”]
if $hidden == 0 then
@households = Household.paginate :per_page => 16,
:page => params[:page],
:order => ‘last_name’,
:conditions => conditions
else
@households = ArchivedHousehold.paginate :per_page => 16,
:page => params[:page],
:order => ‘last_name’,
:conditions => conditions
end
render :partial=>“search”, :layout=>false
end

POST /households

POST /households.xml

PUT /households/1

PUT /households/1.xml

def update
@household = Household.find(params[:id])
@churches = Church.find(:all, :order => “name”).map {|u| [u.name, u.id]}
fix_dates
@v =
Visit.find_or_create_by_household_id_and_month_and_year(:household_id
=> params[:id], :month => @month, :year => @year)
if @week == 1 then
@xweek = @v.week1
@yweek = params[:visit][:week1].to_i
elsif @week == 2 then
@xweek = @v.week2
@yweek = params[:visit][:week2].to_i
elsif @week == 3 then
@xweek = @v.week3
@yweek = params[:visit][:week3].to_i
elsif @week == 4 then
@xweek = @v.week4
@yweek = params[:visit][:week4].to_i
elsif @week == 5 then
@xweek = @v.week5
@yweek = params[:visit][:week5].to_i
end
if @v.monthly != params[:visit][:monthly].to_i || @xweek != @yweek
@today = Date.today
@begin = Date.new(@today.year,7,1)
if @household.start_date < @begin then
@household.start_date = @today
end
end
@v.update_attributes(params[‘visit’])

debugger

if @household.update_attributes(params[:household])
  flash[:notice] = 'Household was successfully updated.'
  redirect_to households_path
else
  flash[:notice] = 'Create failed - look for a red field name'
  @churches = Church.find(:all, :order => "name").map {|u|

[u.name, u.id]}
render :action => “edit”
end
end

def watch_address

debugger

if @match = Household.find_by_address(params[':address'])
  render :partial => "address_match"
else
  render(:nothing=>true)
end

end

def watch_church
# debugger
if (params[’:zip’] != “01540”) && (params[’:zip’] != “01537”) &&
(params[’:zip’] != “01611”) then
render :partial => “church_select”
else
render(:nothing=>true)
end
end
end

Index.html.erb

Index of Households

<%= text_field_tag 'search', params['search'], :autocomplete => "off" %> <%= observe_field 'search', :frequency => 0.5, :update => 'searchdiv', :url => {:controller => 'households', :action => 'search'}, :with => "'search=' + encodeURIComponent(value) " %>
<%= render :partial=>"search" %>
<% form_tag new_household_path do%>
<%= if $hidden == 0 then submit_tag "New household" end %>
<% end %>
<%= will_paginate @households %>

<%= link_to ‘Admin’, admin_path %> <%= if $hidden == 1 then link_to
“Back”, { :action => :back } end %>


<%= javascript_tag(’$(“search”).focus();’) %>

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs