Hobo / hobo

The web app builder for Rails (moved from tablatom/hobo)
http://hobocentral.net
103 stars 39 forks source link

Use "references" to build associations? #106

Open JezC opened 10 years ago

JezC commented 10 years ago

Rails 3.2 and up (maybe earlier) allows:

rails g model thing stuff:references

The references type is an id to another table - stuff in this example.

If I do:

hobo g (resource|model) thing stuff:references

I get a thing.rb that described a field of type references. But that's apparently not recognised as anything special. Instead, I think it should produce a line:

belongs_to :stuff, inverse_of: stuffs

Harder to justify building the other end - has_one? has_many?

And if it is a join table... could add both belongs_to statements, and suppress the join table id?

That, BTW, is an interesting puzzle I haven't solved. How does one suppress the id in a join file in Hobo? In Rails, I'd ad an option to the table - "id: false". Can't see a Hobo option to do that.

iox commented 10 years ago

Hi Jeremy,

I'm sorry, but I am not very sure of what you meant with this sentence: The references type is an id to another table - stuff in this example.. What does "type" mean in this context?

I also didn't understand How does one suppress the id in a join file in Hobo?. What's a "join file"?

Could you be looking for this syntax?

hobo g resource thing bt:stuff

This creates a "belongs to" relationship while creating the resource. We also have a "has many" syntax, using hm instead of bt.

JezC commented 10 years ago

Yeah, imprecise use of language. Sorry 'bout that. Wasn't aware of the bt and hm syntax. I'll go look into those! Thanks!

"references type" should be "references variable type identifier". Or "attribute". When you use "stuff:references", you're telling Rails that you want a field called "stuff_id" which will be used in a belongs_to relationship. We might be able to trivially handle this using the Rich Types. I haven't investigated, but that looks as if it might work. Perhaps.

If I have have two models and I want to join them via a join table, I don't want an ID on the join table. The migration file in Rails would normally have "id: false" in the table parameters. But... what does one do in Hobo? The migration file is generated from the models. The models would each have a "has_many" to the join table. One could generate a Hobo join model as:

hobo g model stuff_thing stuff:references thing:references

I can't see a way to suppress the ID field. The model file specifying the join table should just be:

fields do end attr_accessible :stuff_id, :thing_id belongs_to :stuff, inverse_of: :stuff_thing belongs_to :thing, inverse_of: :stuff_thing

Note - I'm suppressing timestamps with a manual edit.

That, after a hobo g migration, results in:

create_table "stuff_things", force: true do |t| t.integer "stuff_id" t.integer "thing_id" end

Possibly the right thing to do (I feel a "doh" moment coming on, strongly) is to create the join table as a Rails model - no "hobo_model" and no hobo extensions. Just use a "rails g stuff_thing stuff:references thing:references". And let rails build the migration the way that Rails wants it. But that feels like... abdicating responsibility in the builder.

There's no parameter that I can see (in docs - perhaps there is in the source?) to suppress the ID in Hobo. The join table should include an "id: false", somehow:

create_table "stuff_things", id: false, force: true do |t| t.integer "stuff_id" t.integer "thing_id" end

JezC commented 10 years ago

OK, I can see a lighthouse issue where there's discussion of 'bt' and 'hm'. I think that predates the rails 'references' usage. It's also an odd syntax, apparently preceding model name. The rails syntax behaves like the normal field definition. OTOH, 'references' only implies a "belongs_to" association.