Hello I’m making a form for establishing many-to-many relationship between two models
using this solution
I Have Three Model
Artist, Article, ArtistRelationship.
This is Artist Model.
class Artist < ActiveRecord::Base
default_scope order('created_at DESC')
attr_accessible :body_en, :body_kr, :title_en, :title_kr
has_many :articles, :through => :artist_relationships
has_many :artist_relationships
end
And this is Article Model.
class Article < ActiveRecord::Base
default_scope order('created_at DESC')
attr_accessible :title, :body, :date
has_many :artists, :through => :artist_relationships
has_many :artist_relationships
end
And this is ArtistRelationship Model
class ArtistRelationship < ActiveRecord::Base
default_scope order('created_at DESC')
attr_accessible :article_id, :artist_id
belongs_to :artist
belongs_to :article
end
Now I have a form for an article to set the artists.
<%= form_for [:admin, article] do |f| %>
.....
<% Artist.all.each do |artist| %>
<div>
<%= label_tag :artist_ids, artist.title_kr %>
<%= check_box_tag :artist_ids, artist.id, article.artists.include?(artist), :name => 'article[artist_ids][]' %>
</div>
<% end %>
The error come from here
article.artists.include?(artist)
It raises these error
Mysql2::Error: Column ‘created_at’ in order clause is ambiguous: SELECT 1 AS one FROM
artistsINNER JOINartist_relationshipsONartists.id=artist_relationships.artist_idWHEREartist_relationships.article_id= 1 ANDartists.id= 2 ORDER BY created_at DESC LIMIT 1
I can’t get what it means..
And weired thing occurs when I use pry to debug right above the code
<%= check_box_tag :artist_ids, artist.id, article.artists.include?(artist), :name => 'article[artist_ids][]' %>
in pry console when I directly call article.artists.include?(artist)
It raise same error as I expected. but
When I call article.artists it returns an array of artist belongs to that article
then I call article.artists.include?(artist) again. I works fine.
What’s the problem of this?
in all your default scope, include the tableized version of the class