Machinist 2 - Associations: Am I doing them right?

39 views
Skip to first unread message

samullen

unread,
Aug 23, 2010, 1:34:10 PM8/23/10
to Machinist Users
Just started using Machinist2 with Rails3. I'd really like some
feedback so I know whether I'm doing things correctly, or to know if
there is a more smarter way of doing things.

I'm dealing with four tables: clients, ip_whitelists, products, and
subscriptions.
- Clients have many subscriptions.
- Products have many subscriptions
- Subscriptions have many ip_whitelists

Under the "Associations" section of the "Blueprints" wiki page, it
said to drop in the object association, but that really didn't work. I
ended up with a recursion explosion.

Here's what I did instead:

describe IpWhitelist do
def generate_whitelist
@client = Client.make!
@product = Product.make!
subscription = Subscription.make!({:client => @client, :product =>
@product})
@whitelist = IpWhitelist.make!({:subscription => subscription})
end
def destroy_whitelist
Subscription.destroy_all
Client.delete_all
Product.delete_all
end

describe "ip_whitelist validation" do
before :each do
generate_whitelist
end

it "should be awesome" do

... Specy code goes here ...

end
end

after :all do
destroy_whitelist
end
end

--------------------------------------------------
This all works and that's all well and good, but I want to make sure
I'm doing things all proper-like. Feedback would be greatly
appreciated.

Thanks,
Samuel

Pete Yandell

unread,
Aug 23, 2010, 8:14:08 PM8/23/10
to Machinist Users


On Aug 24, 3:34 am, samullen <samul...@gmail.com> wrote:
> Under the "Associations" section of the "Blueprints" wiki page, it
> said to drop in the object association, but that really didn't work. I
> ended up with a recursion explosion.

Not sure where it says that, or what you mean.

You should be able to do:

IpWhiteList.blueprint do
subscription
end

Subscription.blueprint do
client
product
end

Client.blueprint do
end

Product.blueprint do
end

Then you can just do:

IpWhiteList.make!

- Pete

samullen

unread,
Aug 24, 2010, 8:19:00 AM8/24/10
to Machinist Users
Pete,

Thanks. I realized what I was doing wrong shortly after posting my
initial query. I had gotten it into my head that the models
representing the "many" *had* to contain references to the "one", and
the models representing the "one" *had* to contain references to the
"many". That caused my explosion, "but uh... everything's perfectly
all right now. We're fine. We're all fine here now, thank you. How are
you?"

So for what it's worth, thanks for Machinist and all the work you put
into it. And thanks for the time you take to respond on the newsgroup.

Samuel
Reply all
Reply to author
Forward
0 new messages