Ruby on Rails Monday, May 28, 2012

Thank you for your fast reply!
Yes, I agree it isn't necessary to put a direct relation to country inside the city model. But I did it because sometimes the user will not know to which state a new city belongs to, and at least he will know the country.

Luis

Em segunda-feira, 28 de maio de 2012 03h27min39s UTC-3, Colin Law escreveu:

On 28 May 2012 04:37, LuisRuby wrote:
> Hi friends!
> I'm relatively new with Rails and I'm struggling for a long time with this
> problem (it should have a pattern solution but until now I didn't find it):
> I have the following models: Institution, City, State and Country.
>
> class Country < ActiveRecord::Base
>   has_many :states
>   has_many :cities, :through => :states
> end
> # == Schema Information
> # Table name: countries
> #  id         :integer         not null, primary key
> #  sigla      :string(2)       not null
> #  nome       :string(30)      not null
>
> class State < ActiveRecord::Base
>   has_many   :cities
>   has_many   :institutions, :through => :cities
>   belongs_to :country
> end
> # == Schema Information
> # Table name: states
> #  id         :integer         not null, primary key
> #  country_id :integer     not null
> #  sigla      :string(2)      not null
> #  nome       :string(40)  not null
>
> class City < ActiveRecord::Base
>   has_many :institutions
>   belongs_to :country

That is not right, a cities country is accessible through the state
via @city.state.country.

>   belongs_to :state
> end
> # == Schema Information
> # Table name: cities
> #  id          :integer         not null, primary key
> #  country_id  :integer         not null

Above is not needed.

I have not even read the rest, you have provided much too much
information.  If you still have a problem then simplify it down to to
the smallest amount of code that demonstrates it and post that.

Colin

> #  state_id    :integer         not null
> #  nome        :string(40)      not null

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/OFqhnkPSj8gJ.
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.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment