Creating a blueprint for a belong_to with foreign_key

29 views
Skip to first unread message

map7

unread,
Mar 16, 2011, 7:54:21 PM3/16/11
to Machinist Users
I'm using Rails 3 with machinist 2 and I have the following blueprint

Staff.blueprint do
username { "t946" }
email { "mic...@dtcorp.com.au"}
password { "password" }
default_company { company }
end

In my staff model I have declared my belongs_to with the following:
belongs_to :default_company, :class_name => "Company"

In my staff table I have a field called 'default_company_id' set as an
integer.

Is it possible to get machinist to link using a different foreign_key
than the default?

Pete Yandell

unread,
Mar 17, 2011, 10:26:15 PM3/17/11
to Machinist Users
I'm not sure I understand the question.

On Mar 17, 10:54 am, map7 <map7...@gmail.com> wrote:
> I'm using Rails 3 with machinist 2 and I have the following blueprint
>
> Staff.blueprint do
>   username { "t946" }
>   email    { "mich...@dtcorp.com.au"}
>   password { "password" }
>   default_company { company }
> end

Where's the "company" coming from in your default_company line?

> In my staff model I have declared my belongs_to with the following:
>   belongs_to :default_company, :class_name => "Company"
>
> In my staff table I have a field called 'default_company_id' set as an
> integer.
>
> Is it possible to get machinist to link using a different foreign_key
> than the default?

How does this relate to the above, given default_company_id is the
default foreign key for that association?

Machinist just works with whatever foreign keys and classes you define
in your associations.

- Pete

map7

unread,
Mar 20, 2011, 7:02:56 PM3/20/11
to Machinist Users
Here is my full blueprints.rb file:

require 'machinist/active_record'

# https://github.com/notahat/machinist/wiki/Blueprints

Company.blueprint do
name { "Digitech" }
acn { "12345678901" }
end

Staff.blueprint do
username { "t946" }
email { "mic...@dtcorp.com.au"}
password { "password" }
default_company { company }
end

Here is my full staff model:
class Staff < ActiveRecord::Base

devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable


attr_accessible :username, :email, :password, :password_confirmation, :remember_me

has_many :employees
has_many :companies, :through => :employees

belongs_to :default_company, :class_name => "Company"
end

And here is my full Company model
class Company < ActiveRecord::Base
has_many :ledgers

has_many :employees
has_many :staff, :through => :employees

define_index do
indexes :name

set_property :delta => true
end
end

Sorry for not providing enough information in my first post.

If I have the following in my blueprint should this fill in the
association 'default_company_id' in the staff model?

Do I have to call my field 'company_id' instead of
'default_company_id' in the staff table for this automatic association
to work?

Staff.blueprint do
company
username { "t946" }
email { "mic...@dtcorp.com.au"}
password { "password" }
end

Instead of doing this link in my blueprints I've been doing it in my
cucumber steps (via Pickle). Is this a better way of creating such a
link?

from
Mick
Reply all
Reply to author
Forward
0 new messages