Domanda

Basically, all I need is a template_id field in my business table to be assigned correctly so that if I did Business.first.template it would return the result of the current assigned template for that business. At the moment I get I am just getting 'nil'

In my project a Business belongs_to a template (I believe this puts the primary key in the template table and the foreign key in the business table).

class Business < ActiveRecord::Base
belongs_to :template

class Template < ActiveRecord::Base
has_many :businesses

When the user fills out a form for a 'new business' they select the template they wish to use. The templates table is already filled with 3 templates, template_id, 0, 1, 2 (so I cant really work out if anything needs to be 'created'). The user is limited through the form to select only one of 3 templates (radio buttons).

When submitting the form and creating the business the link between the business and the template is currently not created. I don't have anything about the template creation in my business class because I cant work out what would need to be created, the template records already exist in the template table and are static.

Business Controller
  def new
    @business = current_user.businesses.build
    @business.addresses.build
  end

  # POST /businesses
  def create
    @business = Business.new(business_params)
    @business.users << current_user
    if @business.save
      redirect_to @business, notice: 'Business was successfully created.'
    else
      render action: 'new'
    end
  end

  def business_params
    params.require(:business).permit(:name, :email, :template_id, addresses_attributes [:number, :street, :suburb, :state, :country], template_attributes: [:name])

I am not sure if I should be assigning template_id myself or doing something with 'build_template'

Schema

  create_table "businesses", force: true do |t|
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "name"
    t.string   "description"
    t.string   "sub_heading"
    t.string   "email"
    t.integer  "template_id"
  end

  create_table "templates", force: true do |t|
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "name"
    t.integer  "cost"
  end

I am not sure if I should be assigning the value as either 0, 1 or 2 directly from the form submitted by the user to template_id in the business table or if I should be allowing nested attributes as I did with the addresses table.

Any help is appreciated. Thanks

È stato utile?

Soluzione

The foreign key to template id will be fine though. It is what ties an instance of Business to a and instance of Template.

You aren't creating a template, you are selecting one already from a list of created templates. You can access a business's template should be as simple as Business.find(id).template where Id is the id of the business you want knowledge about.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top