Ruby on Rails Sunday, July 29, 2012

On Sunday, July 29, 2012 12:08:09 AM UTC+1, Ruby-Forum.com User wrote:

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:

view

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

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



controller

  if (params[:state] )
        statesmen = State.find_by_name(params[:state][:name]).users
        statesmen.each do |person|
          @photos = Photo.where(:user_id => person.id)
        end
  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?

 
If you added a has_many :photos, :through => :users associations to State then you should be able to do state.photos
 
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 the individuals results together

Fred
--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/pzyGQWaxYJkJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment