How complex are Rails models? sample projects?

I’m trying to get past the beginning stage of Rails and in the middle, but it’s hard for me to find more complex examples to learn.

For example, I read that you need to be careful about Nested Routes and should not occupy more than two depths. What happens in such a situation?

  • The client can place many orders
  • Orders can have many items.
  • Elements can have many types of options.
  • Each type of option may have limitations: available on certain days or requires a choice or affects the total price, etc.

Is this a crazy errand or simple things for Rails. I assume the latter, but I can’t find interesting projects (source) from which to learn? Books seem to stop at the basics ... of ideas?

+3
source share
3 answers

You can go as deep as you like, nested routes, but keep in mind that just because you can doesn’t mean what you need. The deeper you dig, the more work you create for yourself.

I saw that for each depth level you need to create a base controller that processes the parent parameters, and a subclass that processes the specifics. This tends to reproduce along the lines of:

Customer::BaseController < ApplicationController
CustomerController < CustomerController:: BaseController

Customer::Orders::BaseController < Customer::BaseController
Customer::OrdersController < Customer::Orders::BaseController

Customer::Orders::Items::BaseController < Customer::Orders::BaseController
Customer::Orders::ItemsController < Customer::Orders::Items::BaseController

BaseController in each case handles the loading and interpretation of parameters in a general way, for example:

class Customer::BaseController < ApplicationController
  before_filter :load_customer

protected
  def load_customer
    @customer = Customer.find(params[:customer_id] || params[:id])
  rescue ActiveRecord::RecordNotFound
    render(:partial => 'customer_not_found', :layout => 'application', :status => :not_found)
  end
end

As you can see, it can get a little confused if you draw your application this way. You also get very long routes.

, , , . @order.customer client_id .

+2

, , .

, . , . , - .

"Agile Web Development with Rails". .

, "" "" , .

, , , , . , , , , google , ... , .

. , . , , .

: http://github.com - , , .

+1

Look at opensourcerails.com - find the application that interests you, take the source and go through it in parts. Run it locally, find interesting functionality, and then enter the code to see how it is done.

0
source

Source: https://habr.com/ru/post/1757208/


All Articles