ruby on rails - undefined method 'article' - use data from other model -
there articles , comments.
article has_many :comments comment belongs_to :article
i want comments value_id equal value_id attribute in article comment belongs_to.
class comment < activerecord::base belongs_to :article def self.value_comments where(value_id: self.article.value_id) end end
i error:
undefined method `article' #<class:0x007fd2a7e46d18>
controller
@value_comments = comment.value_comments.where(user_id: current_user.id).order("created_at desc")
having read question again, understanding want find comments
value_id
matches value_id
of associated article
.
your code correct - need few more parts work. need join comment
table article
table using joins
. then, refer column in function using arel_table
.
so should end this:
def self.value_comments joins(:article).where(self.arel_table[:value_id].eq article.arel_table[:value_id]) end
you consider using sexy_scopes make easier access columns.
Comments
Post a Comment