vhochstein / active_scaffold

Rails 4 Version of activescaffold supporting jquery
MIT License
156 stars 34 forks source link

form field for :through associations #76

Closed markmcdonald51 closed 13 years ago

markmcdonald51 commented 13 years ago

Hi,

I noticed that :through associations such as :users (listed below) are not available in an update as these are designated read_only. Is there anyway around this if I wanted to create a select list using this through assoc?


class Group < ActiveRecord::Base belongs_to :company has_many :affiliations has_many :users, :through => :affiliations end

Thanks in advance. Mark

vhochstein commented 13 years ago

affilitiations model has many attributes or is it just a group_id and user_id table?

markmcdonald51 commented 13 years ago

class Affiliation < ActiveRecord::Base belongs_to :user belongs_to :company belongs_to :group end

vhochstein commented 13 years ago

If you could remove company from Affiliation you could change it to a has_and_belongs_to_many association which is writeable.

If not it s get a little bit more complex, cause through assocations are by design read only associations in activerecord, which means you can only edit/create has_many association to affiliations.

So one option would be to define to_label method of affiliation model to show the user?

If you don t like that because to_label will be used everywhere you might create a custom form column helper for that association, where you show user name in select box.

markmcdonald51 commented 13 years ago

Yeah. It is actually a bit more than that. As the table definition is probably going to get a bit more hairy in the future (maybe adding in aasm states and such). At present it looks like:

create_table :affiliations do |t| t.references :user t.references :company t.references :group t.string :role

I really like using Active Scaffold for some stuff but the problem is when you need to add in something complex it gets a little scruffy. I was using AS for some pretty large stuff where we had to use a hack saw to get it to do the right thing. It's a great tool for prototyping --nice and easy to look at simple data.

Thanks for the response.

vhochstein commented 13 years ago

Well, you are most definetly right, that complex stuff remains complex even with Activescaffold... :-)

However, I think rails 3 version improves this at least in some areas and I would nt call a form override complex.

markmcdonald51 commented 13 years ago

So wait, you're saying I could just create a form override and use the :through association?

You're right. This isn't complex but it does need to be a :through association which I thought was unsupported by AS (right?). So if I did create the form override for :users would it even update on the backend?

vhochstein commented 13 years ago

Nope, you are right I ve mixed that up with another use case. Let me think about this for a few days. Through associations are on my open feature list.