Selecting From A Different Table

How do I select records from a table that has a different name than my
controller?

My example is as follows

in locations_controller.rb

class LocationsController < ApplicationController

def new
  @location = Location.new
  @regions = #Needs to do a find(:all) to populate from regions table
end

I believe you are looking for:

   @regions = Region.find(:all)

Assuming I guess your model name correctly. Just remember that find() is just a method call on some model.

Hope that helps.

James Edward Gray II

···

On Oct 3, 2005, at 11:31 AM, Paul Thomas wrote:

How do I select records from a table that has a different name than my
controller?

My example is as follows

in locations_controller.rb

class LocationsController < ApplicationController

def new
  @location = Location.new
  @regions = #Needs to do a find(:all) to populate from regions table
end

James Edward Gray II wrote:

> How do I select records from a table that has a different name than my
> controller?
>
> My example is as follows
>
> in locations_controller.rb
>
> class LocationsController < ApplicationController
>
> def new
> @location = Location.new
> @regions = #Needs to do a find(:all) to populate from regions table
> end

I believe you are looking for:

   @regions = Region.find(:all)

Assuming I guess your model name correctly. Just remember that find
() is just a method call on some model.

Hope that helps.

James Edward Gray II

Thanks for the prompt reply. When I call @regions = Region.find(:all)
the @regions is empty. Region is my model name as well.

Here are my models that are of importance to this.

class Region < ActiveRecord::Base
  has_one :location
end

class Location < ActiveRecord::Base
  belongs_to :region
end

And from the controller
def new
  @location = Location.new
  @regions = Region.find(:all) # @regions is still empty
end

And lastly, from the view (_form.rhtml -- used in edit.rhtml and
new.rhtml)
<% collection_select("location", "region_id", @regions, "id", "name") %>

···

On Oct 3, 2005, at 11:31 AM, Paul Thomas wrote:

Try putting <%= debug(@regions) %> on your _form.rhtml, above the
collection_select call.
I believe you'll find that the regions are there, it's just that your
select call isn't quite right. (Should be <%=, not <%, by the way)

Try this:
def new
@location = Location.new
@regions = Region.find(:all)
@region_options = @regions.zip(@regions)
end

..and then, in the form:
<%= select('location', 'region_id', @region_options) %>

--Wilson.

···

On 10/3/05, Paul Thomas <MrPaulAR@gmail.com> wrote:

James Edward Gray II wrote:
> On Oct 3, 2005, at 11:31 AM, Paul Thomas wrote:
>
> > How do I select records from a table that has a different name than my
> > controller?
> >
> > My example is as follows
> >
> > in locations_controller.rb
> >
> > class LocationsController < ApplicationController
> >
> > def new
> > @location = Location.new
> > @regions = #Needs to do a find(:all) to populate from regions table
> > end
>
> I believe you are looking for:
>
> @regions = Region.find(:all)
>
> Assuming I guess your model name correctly. Just remember that find
> () is just a method call on some model.
>
> Hope that helps.
>
> James Edward Gray II

Thanks for the prompt reply. When I call @regions = Region.find(:all)
the @regions is empty. Region is my model name as well.

Here are my models that are of importance to this.

class Region < ActiveRecord::Base
  has_one :location
end

class Location < ActiveRecord::Base
  belongs_to :region
end

And from the controller
def new
  @location = Location.new
  @regions = Region.find(:all) # @regions is still empty
end

And lastly, from the view (_form.rhtml -- used in edit.rhtml and
new.rhtml)
<% collection_select("location", "region_id", @regions, "id", "name") %>

Thank you. The root of my problem was the missing =.