Forum: Ruby on Rails db setup,active record

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
B21ece81d091fbff15d8c70b394ebeb0?d=identicon&s=25 los x. (los_x)
on 2012-07-29 01:08
my app has user, photo, and state models.
a user belongs to a state and thusly has a state_id field,
a user has many photos.
I have the photos view set up as the home page.
I am trying to set up a select tag and form tag to filter through the
photos by which state is selected.
My files are:


  <%= form_tag photos_path, :method => 'get' do%>

          <%= select("state", "name", State.all.collect(&:name)) %>
    <%= submit_tag "State" %>
  <% end %>


  if (params[:state] )
        statesmen = State.find_by_name(params[:state][:name]).users
        statesmen.each do |person|
          @photos = @photos <<  Photo.where(:user_id =>
  else .....

My questions are 1
Is there a better way to collect the photos?  Theres gotta be.  Should I
just give the photo a state_id field?

and 2
Why can't I do it this way
Frederick Cheung (Guest)
on 2012-07-29 11:10
(Received via mailing list)
On Sunday, July 29, 2012 12:08:09 AM UTC+1, User wrote:
>   if (params[:state] )
If you added a has_many :photos, :through => :users associations to
then you should be able to do

> and 2
> Why can't I do it this way
> Well you haven't said what's not working, but at a guess it's because
you're just assigning to @photos again and again rather than addinging
individuals results together

This topic is locked and can not be replied to.