기본적으로 내 비즈니스 테이블의 template_id 필드를 올바르게 할당하면 Business.first.template을 수행하면 해당 비즈니스에 대해 할당 된 현재 템플릿의 결과가 반환됩니다. 그 순간에 나는 단지 '무관'을 얻는다.belongs_to 연관에 외래 키 채우기
내 프로젝트에서 비즈니스는 템플릿을 소유한다. (템플릿 테이블에 기본 키가 있고 비즈니스 테이블에 외래 키가 있다고 생각한다.)
class Business < ActiveRecord::Base
belongs_to :template
class Template < ActiveRecord::Base
has_many :businesses
사용자가 '신규 비즈니스'양식을 작성하면 사용할 템플릿을 선택합니다. 템플릿 테이블은 이미 3 개의 템플릿, template_id, 0, 1, 2로 채워져 있습니다. (따라서 '생성해야'할 필요가있는 경우 실제로 작동하지 않습니다.) 사용자는 3 개의 템플릿 (라디오 버튼) 중 하나만을 선택하기 위해 양식을 통해 제한됩니다.
양식을 제출하고 비즈니스를 생성 할 때 현재 비즈니스와 템플릿 간의 링크가 생성되지 않습니다. 내 비즈니스 클래스에서 템플릿을 만드는 것에 대해서는 아무 것도 만들지 못합니다. 왜냐하면 만들 필요가있는 것을 해결할 수 없기 때문에 템플릿 테이블에 이미있는 템플릿 레코드가 정적이며 정적입니다.
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])
나는 같은 하나의 값을 할당 할 필요가있는 경우 나 자신을 template_id 할당하거나 'build_template'
스키마와 함께 일을하고
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
나는 확실하지 않다해야하는지 확실하지 않다 0, 1 또는 2 직접 비즈니스 테이블에 template_id 사용자가 제출 한 양식 또는 내가 주소 테이블을 사용한 것처럼 중첩 된 특성을 허용해야합니다.
도움을 주시면 감사하겠습니다. 감사합니다
고마워요! 내가 필요한 모든 것. 나는 SQL을 쓰는데 너무 익숙하다. 때로는 능동적 인 레코드로 그렇게 간단한 것을하는 것이 어렵다. – Rj01