i have three tables and want to connect them. so is it stupid to make a
triple m:n (rails style xs_ys_zs) or do i have to make a new table
(newtable) where i got multiple 1:n?
the habtm (has and belongs to many) do only work proper to join two
tables, or i am wrong?
i have three tables and want to connect them. so is it stupid to make a
triple m:n (rails style xs_ys_zs) or do i have to make a new table
(newtable) where i got multiple 1:n?
It depends on whether you want the join table to have attributes. If you
do, or think you might in the future, then you need a join model (with
the appropriate table). It’s a little messier, but has_many :through
coming in Rails 1.1 makes life easier.
Otherwise, you can get away with HABTM.
the habtm (has and belongs to many) do only work proper to join two
tables, or i am wrong?
i have three tables and want to connect them. so is it stupid to make a
triple m:n (rails style xs_ys_zs) or do i have to make a new table
(newtable) where i got multiple 1:n?
It depends on whether you want the join table to have attributes. If you
do, or think you might in the future, then you need a join model (with
the appropriate table). It’s a little messier, but has_many :through
coming in Rails 1.1 makes life easier.
Otherwise, you can get away with HABTM.
how i can read attributes stored in the join table?
M:N Table
user_id
link_id
text <- how i can read out this value
updated_at <- how i can read out this value
created_at <- how i can read out this value
is it user.text or link.text?
the habtm (has and belongs to many) do only work proper to join two
tables, or i am wrong?
how i can read attributes stored in the join table?
M:N Table
user_id
link_id
text <- how i can read out this value
updated_at <- how i can read out this value
created_at <- how i can read out this value
is it user.text or link.text?
Just access it as a normal model.
@a = JoinModel.find :first @a.text
You can also access it through the models it belongs to, such as
user.join_models…
and my experiences with login_engine. You might want to consider
acts_as_authenticated.
Thanks Brian,
I currently go away from M:N. I will try acts_as_authenticated. So will
do multiple 1:N on one table like David Heinemeier wrote
(http://article.gmane.org/gmane.comp.lang.ruby.rails/32742/). So I can
use :though when Rails 1.1 comes up.
Rails is really great, but when a complex data model is on duty it’s
anything than easy…
when you want to store M:N’s you have to add this on scaffolding
def create @link = Link.new(params[:link])
if @link.save @link.users = User.find_by_Something @link.update_attributes(params[:link])
flash[:notice] = ‘Link was successfully created.’
redirect_to :action => ‘list’
else
render :action => ‘new’
end
end
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.