[Rails] Re: Active Record has_one through

2015-11-05 Thread Chris Berry
Turns out the event table needed to have an artist_id column.  I was 
attempting to use the join table without a reference to the artist 
table.

I dont know if that was the fix, because i also wrote a function in the 
event model that used the artist_id to return artist data from the 
database.

I did try your suggestion when initially building the classes...but 
again, without the artist_id in the event table, it failed.

class Event < ActiveRecord::Base
  has_many :city_events, dependent: :destroy

  validates :EventID, uniqueness: true

  def artist
Artist.find(artist_id)
  end

  def city
City.find(city_id)
  end

end

-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/1e2f6f614609a96eb1f15c52d26255c7%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.


[Rails] Re: Active Record has_one through

2015-10-30 Thread Travis Eubanks
why dont you just do this


class Artist < ActiveRecord::Base
  has_many :events, dependent: :destroy
end

class Event < ActiveRecord::Base
  belongs_to :artist
end

a = Artist.first
a.events which will return a list of events connected to that artist

e = Event.first
e.artist which will return the artist that is connected to that event.

Maybe explain what your user story is to better understand.  With what 
you posted I comprehended the statement above.

-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/8be9ad7fa3b35df3628035d98ecf187b%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.


[Rails] Re: Active Record has_one through

2015-10-08 Thread Matt Jones


On Wednesday, 7 October 2015 20:47:54 UTC-4, Ruby-Forum.com User wrote:
>
> I have two tables in my app which I am attempting to relate through a 
> join table.  The Artist class which uses 'has_many through', works as 
> expected.  However, the Event class using 'has_one through', brings back 
> nil. 
>
> class Artist < ActiveRecord::Base 
>   has_many :artist_events, dependent: :destroy 
>   has_many :events, through: :artist_events 
> end 
>
> class Event < ActiveRecord::Base 
>   belongs_to :artist_event 

  has_one :artist, through: :artist_events 
>

Something hasn't been copied correctly here, because this shouldn't work - 
the `through` refers to an association (artist_events) that doesn't exist 
on Event.

As noted elsewhere, `belongs_to` is likely not the right association to use 
here; it's expecting an 'artist_event_id' column on the 'events' table.

You'll also want to carefully consider if `has_one` is the right 
association as well. The table structure you've set up (Artist / 
ArtistEvent / Event) is a classic many-to-many relationship. If an Event 
can truly only have one artist, this structure is not needed. If an Event 
can have many artists, than a has_one isn't correct.

--Matt Jones

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/2fa1934c-fe96-42e6-bcb2-fa4575d30f8a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.