Relationship question has_many

2 tables with a has many relationship. Landen can only have one
Werelddeel, and Werelddeel can have more then one Landen.

Question 1: Is this the model setup good?

class Land < ActiveRecord::Base
belongs_to :werelddeel, :foreign_key => “werelddeel_id”
set_table_name “landen”
set_primary_key “landcode”
end

class Werelddeel < ActiveRecord::Base
has_many :land, :foreign_key => “werelddeel_id”
set_table_name “werelddelen”
set_primary_key “werelddeel_id”
end

Question 2:
how can i realize a url structure
domein.com/wereldddeel/afrika/land/south-afrika

Table landen
Field | Type | Null | Key | Default | Extra |
±--------------±--------------------±-----±----±--------±------+
| landcode | char(2) | NO | PRI | NULL | |
| naam | varchar(50) | NO | | NULL | |
| naam_en | varchar(50) | NO | | NULL | |
| alt_naam | varchar(200) | NO | | NULL | |
| landafk | char(3) | YES | | NULL | |
| land_zoeknaam | varchar(100) | NO | MUL | NULL | |
| werelddeel_id | tinyint(2) unsigned | YES | MUL | NULL |

Table werelddelen
±--------------±---------------------±-----±----±--------±---------------+
| Field | Type | Null | Key | Default | Extra |
±--------------±---------------------±-----±----±--------±---------------+
| werelddeel_id | smallint(2) unsigned | NO | PRI | NULL | auto_increment |
| naam | varchar(50) | YES | | NULL | |
| naam_en | varchar(50) | YES | | NULL | |
| zoeknaam | varchar(50) | YES | MUL | NULL | |
| description | varchar(255) | YES | | NULL | |
| permalink | varchar(255) | YES | | NULL | |
±--------------±---------------------±-----±----±--------±---------------+

the belongs_to and has_many is ok so far.

but it is a VERY bad idea to use dutch naming,
since it breaks the pluralization rules of Rails
and forces you to use a lot of explicit declarations.

(tells you a german programmer living/working in Amsterdam,
we NEVER do this and my dutch colleagues are surely as proud
of their language as you may be :slight_smile:

class Countries < ActiveRecord::Base
belongs_to :continent
end

class Continents < ActiveRecord::Base
has_many :countries
end

would be all you need, if you would keep to standards
and this will go on with all you routing, path_helpers and whatsoever…

Thorsten M. wrote:

the belongs_to and has_many is ok so far.

but it is a VERY bad idea to use dutch naming,
since it breaks the pluralization rules of Rails
and forces you to use a lot of explicit declarations.

(tells you a german programmer living/working in Amsterdam,
we NEVER do this and my dutch colleagues are surely as proud
of their language as you may be :slight_smile:

class Countries < ActiveRecord::Base
belongs_to :continent
end

class Continents < ActiveRecord::Base
has_many :countries
end

would be all you need, if you would keep to standards
and this will go on with all you routing, path_helpers and whatsoever…

I agree it is a bad idea to generate controllers/model ect. in my own
language.
But i want to make my url-structure in my own language in case of SEO.
Do you have some url’s to solve this problem.

remco

Thorsten M. wrote:

class Countries < ActiveRecord::Base
class Continents < ActiveRecord::Base

Surely:
class Country < ActiveRecord::Base
class Continent < ActiveRecord::Base
?

In answer to the original question 2, use nested routes (in
config/routes.rb):

map.resources :continents do |continent|
continent.resources :countries
end

or a sub-resource:

map.resources :continents, :has_many => :countries

They will generate the same routes. If you will want to access
countries independently then make sure it has its own resource:

map.resources :countries

Use:
rake routes
to check your routing is what you want it to be.

Remco Z. wrote:

But i want to make my url-structure in my own language in case of SEO.
Do you have some url’s to solve this problem.

Use the routes.rb file to create routes that match your required pattern
and map to appropriate actions…

Mark B. wrote:

Remco Z. wrote:

But i want to make my url-structure in my own language in case of SEO.
Do you have some url’s to solve this problem.

Use the routes.rb file to create routes that match your required pattern
and map to appropriate actions…

He Bush,

Thanks for you reply…can you help me with my view…currently it looks
like this

Landen

    <% @landen.each do |land| %>
  • <%= link_to land.naam, land_path(land)%>
  • <% end %>

But i get the error message:
undefined method `land_path’ for #<ActionView::Base:0xb75eaf30

Thanks…
remco

Remco Z. wrote:

  • <%= link_to land.naam, land_path(land)%>
  • But i get the error message:
    undefined method `land_path’ for #<ActionView::Base:0xb75eaf30

    This will be related to the internal pluralisation and singularisation
    used by Rails (which is why sticking to English words works best).
    Still, you can sort this out using the command:

    rake routes

    This will include a line with a URL matching what you need. There
    should be a method name stub to the left. Add “_path” to that.

    Also, you can use Rails magic and use:

    link_to land.naam, land

    Rails will recognise the object and should generate the correct URL. Of
    course, the non-English words and irregular pluralisation of your table
    name may fox Rails here, too.

    Another approach is to teach Rails how to pluralise your model names.
    Add inflection entries to your environment.rb file to teach Rails the
    relation land<->landen.

    Remco Z. wrote:

    werelddeel_landen GET /werelddeel/:werelddeel_id/landen/:id

  • <%= link_to land.naam, werelddeel_landen_path(land)%>
  • <% end %>

    On http://www.goedkoop-vliegen.nu/werelddeel/noord-amerika

    Link Afghanistan i get:
    http://www.goedkoop-vliegen.nu/werelddeel/afghanistan/landen/noord-amerika

    You are supplying the :id field (in “land”) to werelddeel_landen, but
    you also need to supply a value for :wereldeel_id. I think you can do
    this using:

    link_to land.naam, werelddeel_landen_path(land.werelddeel, land)

    Mark B. wrote:

    Remco Z. wrote:

    werelddeel_landen GET /werelddeel/:werelddeel_id/landen/:id

  • <%= link_to land.naam, werelddeel_landen_path(land)%>
  • <% end %>

    On http://www.goedkoop-vliegen.nu/werelddeel/noord-amerika

    Link Afghanistan i get:
    http://www.goedkoop-vliegen.nu/werelddeel/afghanistan/landen/noord-amerika

    You are supplying the :id field (in “land”) to werelddeel_landen, but
    you also need to supply a value for :wereldeel_id. I think you can do
    this using:

    link_to land.naam, werelddeel_landen_path(land.werelddeel, land)

    Hee…Bush

    It works!!!..thanks…!!!

    check the result > http://www.goedkoop-vliegen.nu/werelddeel

    last question :slight_smile:

    There is a belongs_to relationship between werelddeel (continents) and
    landen (countries). So how can i create a view on the continent Europe
    that only the countries belongs to Europe are visible.

    Grtz…

    Mark B. wrote:

    Remco Z. wrote:

  • <%= link_to land.naam, land_path(land)%>
  • But i get the error message:
    undefined method `land_path’ for #<ActionView::Base:0xb75eaf30

    This will be related to the internal pluralisation and singularisation
    used by Rails (which is why sticking to English words works best).
    Still, you can sort this out using the command:

    rake routes

    This will include a line with a URL matching what you need. There
    should be a method name stub to the left. Add “_path” to that.

    Also, you can use Rails magic and use:

    link_to land.naam, land

    Rails will recognise the object and should generate the correct URL. Of
    course, the non-English words and irregular pluralisation of your table
    name may fox Rails here, too.

    Another approach is to teach Rails how to pluralise your model names.
    Add inflection entries to your environment.rb file to teach Rails the
    relation land<->landen.

    Hi…

    rake routes
    werelddeel_landen GET /werelddeel/:werelddeel_id/landen/:id

    So i put werelddeel_landen in the path:

    <% @landen.each do |land| %>

  • <%= link_to land.naam, werelddeel_landen_path(land)%>
  • <% end %>

    On http://www.goedkoop-vliegen.nu/werelddeel/noord-amerika

    Link Afghanistan i get:
    http://www.goedkoop-vliegen.nu/werelddeel/afghanistan/landen/noord-amerika

    something wrong here!! grgrgrgr

    models:

    class Land < ActiveRecord::Base
    belongs_to :werelddeel, :foreign_key => “werelddeel_id”
    set_table_name “landen”
    set_primary_key “landcode”

    has_permalink :land_zoeknaam

    def to_param
    land_zoeknaam
    end
    end

    class Werelddeel < ActiveRecord::Base
    has_many :land, :foreign_key => “werelddeel_id”
    set_table_name “werelddelen”
    set_primary_key “werelddeel_id”
    has_permalink :zoeknaam

    def to_param
    zoeknaam
    end

    end

    I case of nice url’s (has_permalink, to_param)
    i use http://svn.techno-weenie.net/projects/plugins/permalink_fu/

    i am getting a headache of this problem :frowning:

    Grtz…remco

    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