Question

i'm really new in ruby on rails and i need a hand here. In my models i have documents and conversion_details:

class Document < ActiveRecord::Base
    belongs_to :user
    has_one :origin_details, :class_name => "ConversionDetails", :foreign_key => "origin_id"
    has_many :destination_details, :class_name => "ConversionDetails", :foreign_key => "destination_id"
    attr_accessible :extension, :name, :text
end

class ConversionDetail < ActiveRecord::Base
    belongs_to :origin, :class_name => "Documents"
    belongs_to :destination, :class_name => "Documents"
    attr_accessible :conversionDate, :requestMsg, :responseMsg, :status, :origin_id, :destination_id
end

each conversionDetail has an origin document and a one or many documents associated. Mi questions are:

1) this models are fine? because i can't do a migration with "rails g migration .... docuemnt:reference document:reference" twice

2) when i go to rails console and i do: ConversionDetail.create(origin_id:500,destination_id:1000) the conversion its created BUT it shouldn't because i don't have in documents any id with that's numbers

Please help me, i will be grateful. Thanks to all, you are the best!

PD: Sorry for my really bad english

Était-ce utile?

La solution

It sounds like you're expecting rails to define the foreign key constraint in the database. Rails doesn't do this. The foreign_key on associations simply indicates which database column is to be used to look up the associated records.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top