polymorphic nested associations and streamlined views

1 view
Skip to first unread message

riddim_method

unread,
Dec 15, 2008, 9:03:39 PM12/15/08
to Streamlined
Hi I have a data model that is setup as follows


class User < ActiveRecord::Base do
has_one :bio, :as=> bio_enabled
do

class Student < ActiveRecord::Base
has_one :bio, :as=> bio_enabled
has_one :registration

end

class Spec < ActiveRecord::Base
belongs_to :spec, :polymorphic=>true
end

class Registration < ActiveRecord::Base
belongs_to :student
end

The Bio model above contains first_name, last_name etc and all of the
above works perfectly thanks to the wonder of rails. . However, I'd
like to go one step further and do something like this

class Registration < ActiveRecord::Base
belongs_to :student

# has_one :bio [APPROPRIATE OPTION SYNTAX]
#----or ------
# belongs_to :bio [APPROPRIATE OPTION SYNTAX]

end

It seems like this should be possible but I can't figure whether I
should declare this as a has_one or belongs_to association and what
the right syntax should be.

Mind you I know that I have access to to the bio from the registration
model via the student i.e. @registration.student.bio. However, what I
would like to do is edit or create a registration for a student from
streamlined but have the the students name appear in the drop-down
list that streamlined provides instead of the student_id which not
particularly helpful.

Does rails even support this behavior? I couldn't seem to get it to
work and if so can Streamlined support this type of behavior using the
"fields option" of the user_colums? It seems that when setup the
different views for a model ui in streamlined only fields from direct
associations are available.

.

Thanks for any suggestions

Bonefish

unread,
Dec 15, 2008, 10:27:34 PM12/15/08
to Streamlined
Will the "through" option work for you? I haven't tried with
polymorphism; you might also have to specify :source or :source_type.
:through is only available as an option for has_many and has_one
declarations.

class Registration < ActiveRecord::Base
belongs_to :student
has_one :bio, :through => :student
end

If you can get the associations to work then you should be able to use
"fields" to see what you need.

If you can't get the associations to work the way you want then you
can always override the form and tack on the relevant
@registration.student.bio fields. You'll also have to handle this
yourself in the controller.

Regards,
Kerry

riddim_method

unread,
Dec 16, 2008, 6:00:16 AM12/16/08
to Streamlined
Hi Bonefish

Thanks for your reply.
I feel like I've tried every combination of has_one and belongs_to and
the related attributes. I think modifying the view myself will b the
only option partly, because I think I would have to change the
relationship between my models quite a bit and streamlined doesn't
really support editing via associations anyway.

Thanks again.
Reply all
Reply to author
Forward
0 new messages