problem with association and specific key

22 views
Skip to first unread message

Sylvain

unread,
Jul 1, 2009, 5:53:28 AM7/1/09
to ActiveScaffold : Ruby on Rails plugin
Hi again,
I have two Models has follow:

Class Device
has_many :links, :foreign_key => "src_device_id"
has_many :links, :foreign_key => "dst_device_id"


Class Link
belongs_to :src_device, :class_name => "Device"
belongs_to :dst_device, :class_name => "Device"


When I'm on the "Device" page, if I click on the list of links of a
device in order to see them nested, the request fail because the SQL
request finish with "where links.device_id = XXXX) where XXXX is the
ID of the device instead of something like "where links.src_device_id
= XXXX" or even better (but harder for AS) "(links.src_device_id =
XXXX) OR (links.dst_device_id = XXXX)

Is it normal? and is there a way to change that?

Thanks in advance,

Sylvain

Sergio Cambra .:: entreCables S.L. ::.

unread,
Jul 1, 2009, 6:57:33 AM7/1/09
to actives...@googlegroups.com
On Miércoles, 1 de Julio de 2009 11:53:28 Sylvain escribió:
> Hi again,
> I have two Models has follow:
>
> Class Device
> has_many :links, :foreign_key => "src_device_id"
> has_many :links, :foreign_key => "dst_device_id"

This is wrong, you can't have two associations with the same name, rails will
ignore the first association. If you try in the console (script/console)
Device.first.links you can see SQL generated by rails will be use
dst_device_id.

>
>
> Class Link
> belongs_to :src_device, :class_name => "Device"
> belongs_to :dst_device, :class_name => "Device"

There was a bug in ActiveScaffold and in this case ActiveScaffold failed to
guess the foreign_key (but it worked if you set foreign_key). I have fixed it
now.

> or even better (but harder for AS) "(links.src_device_id =
> XXXX) OR (links.dst_device_id = XXXX)

It's not possible because first association is overwritten with second one, so
rails and ActiveScaffold don't know links association with foreign_key
src_device_id.

>
> Is it normal? and is there a way to change that?
>
> Thanks in advance,
>
> Sylvain
>
--
Sergio Cambra .:: entreCables S.L. ::.
Mariana Pineda 23, 50.018 Zaragoza
T) 902 021 404 F) 976 52 98 07 E) ser...@entrecables.com

Sylvain

unread,
Jul 1, 2009, 8:10:27 AM7/1/09
to ActiveScaffold : Ruby on Rails plugin


On 1 juil, 12:57, "Sergio Cambra .:: entreCables S.L. ::."
<ser...@entrecables.com> wrote:
> On Miércoles, 1 de Julio de 2009 11:53:28 Sylvain escribió:
>
>
> This is wrong, you can't have two associations with the same name, rails will
> ignore the first association. If you try in the console (script/console)
> Device.first.links you can see SQL generated by rails will be use
> dst_device_id.

Ok, I didn't knew. Is it possible to achieve it?

>
> > Class Link
> > belongs_to :src_device, :class_name => "Device"
> > belongs_to :dst_device, :class_name => "Device"
>
> There was a bug in ActiveScaffold and in this case ActiveScaffold failed to
> guess the foreign_key (but it worked if you set foreign_key). I have fixed it
> now.

OK, cool.

Sylvain

unread,
Jul 1, 2009, 9:44:00 AM7/1/09
to ActiveScaffold : Ruby on Rails plugin


On 1 juil, 12:57, "Sergio Cambra .:: entreCables S.L. ::."
<ser...@entrecables.com> wrote:
> On Miércoles, 1 de Julio de 2009 11:53:28 Sylvain escribió:
>
> > Hi again,
> > I have two Models has follow:
>
> > Class Device
> > has_many :links, :foreign_key => "src_device_id"
> > has_many :links, :foreign_key => "dst_device_id"
>
> This is wrong, you can't have two associations with the same name, rails will
> ignore the first association. If you try in the console (script/console)
> Device.first.links you can see SQL generated by rails will be use
> dst_device_id.
>

One question about that,
I've done taht in order to be OK with rails:

Class Device
has_many :src_links, :class_name => "Link", :foreign_key =>
"src_device_id"
has_many :dst_links, :class_name => "Link", :foreign_key =>
"dst_device_id"

Now I would like to have column 'Link' which would behave like a
normal "has_many" columns (i.e. ability to click and to see the links)

So first I did that:

def links
src_links + dst_links
end

But that doesn't do what I want...
Is there any other way to do that?

Thanks in advance,

Sylvain

unread,
Jul 1, 2009, 12:14:32 PM7/1/09
to ActiveScaffold : Ruby on Rails plugin
I'm trying to have a solution via the use of helpers.
In DeviceHelper,
I've created a function links_column. The goal is to give link for the
right Links Table, in nested mode. For that, I first recreate the
good
view and add a link. The link is wworking correctly in a non nested
mode.
In a nested mode, I've got an error:
ActiveScaffold::MalformedConstraint in LinksController#show_list
Malformed constraint `Link#aspen_topo_file_id'. If it's a legitimate
column, and you are using a nested scaffold, please specify or double-
check the reverse association name.
This is coming from vendor/plugins/active_scaffold/lib/active_scaffold/
actions/nested.rb:33:in `include_habtm_actions'

I have a Model named AspenTopoFile but the relationship between this
Model and the Link model is a has_many/belongs_to one. So I don't
understand why this error is raised...

Do you know why?

Here is my helper method:

def links_column(record)
if record.src_links.count + record.dst_links.count > 3
if record.src_links.count >= 3
"<a href=/links/show_list/#{record.id}?_method=get&eid=#{params
[:eid]}&nested=true&parent_model=Device>" + record.src_links.first
(3).collect{|ac| h(ac.name)}.join(', ') + " ... (" +
(record.src_links.count + record.dst_links.count).to_s + ")" + "</a>"
else
i = record.src_links.count
"<a href=/links/show_list/#{record.id}?_method=get&eid=#{params
[:eid]}&nested=true&parent_model=Device>" + record.src_links.first
(3).collect{|ac| h(ac.name)}.join(', ') + record.dst_links.first(3-
i).collect{|ac| h(ac.name)}.join(', ') + " ... (" +
(record.src_links.count + record.dst_links.count).to_s + ")"
end
else
if record.src_links.count >= 3
"<a href=/links/show_list/#{record.id}?_method=get&eid=#{params
[:eid]}&nested=true&parent_model=Device>" + record.rc_links.first
(3).collect{|ac| h(ac.name)}.join(', ')
else
i = record.src_links.count
"<a href=/links/show_list/#{record.id}?_method=get&eid=#{params
[:eid]}&nested=true&parent_model=Device>" + record.src_links.first
(3).collect{|ac| h(ac.name)}.join(', ') + record.dst_links.first(3-
i).collect{|ac| h(ac.name)}.join(', ')
end
end
end

Sylvain

unread,
Jul 2, 2009, 5:45:25 AM7/2/09
to ActiveScaffold : Ruby on Rails plugin
Ok I solved it using an action like url instead of a nested url and
that works!
Reply all
Reply to author
Forward
0 new messages