Rails 4,SQ Lite 3:通过没有关联的模型访问模型

时间:2016-10-18 11:23:11

标签: ruby-on-rails ruby database sqlite ruby-on-rails-4

我有两张桌子:

项目
配方

在此示例中,我有一个ID item的项目对象1item表有一列名为recipe_id。对于此对象,它具有值2

recipe有一个名为title的列。

如果我有项目,如何title recipe iditem相关联的= item.recipe_id.title ?{ 有点像......?

public class Latest_News extends Fragment {

private RecyclerView recyclerView;
private List<NewsItems> newsItemsList = new ArrayList<>();
private static final String TAG = Latest_News.class.getSimpleName();
private NewsItems newsItems;

public Latest_News() {
    // Required empty public constructor
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    networkAvailable();
}

@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View mView = inflater.inflate(R.layout.fragment_latest__news, container, false);

    recyclerView = (RecyclerView) mView.findViewById(R.id.latestNews_recyclerView);
    recyclerView.setHasFixedSize(true);

    LinearLayoutManager llMan = new LinearLayoutManager(getContext());
    llMan.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(llMan);

    //recyclerView.setAdapter(new HomeFrag_RecyclerAdapter(getActivity(), newsItemsList));

    if (networkAvailable()){

        String url = "www.goal.com/api";  // fake url, pls dont mind
        RequestQueue mRequestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONObject newsObject = new JSONObject(response.toString());

                            String title = newsObject.getString("title");
                            String time = newsObject.getString("time");
                            String date = newsObject.getString("date");
                            String link = newsObject.getString("link");
                            String content = newsObject.getString("content");
                            String image = newsObject.getString("image" + "");

                            newsItems = new NewsItems();

                            newsItems.setImage_Id(Integer.parseInt(image));
                            newsItems.setNewsDate(date);
                            newsItems.setNewTitle(title);
                            newsItems.setNewsDesc(content);
                            newsItems.setNewsUrl(link);
                            newsItems.setNewsTime(time);

                            newsItemsList.add(newsItems);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        recyclerView.setAdapter(new HomeFrag_RecyclerAdapter(getActivity(), newsItemsList));

                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.i(TAG,"Error is: " + error.toString());
            }
        });
        mRequestQueue.add(jsonObjectRequest);
    }
    else{
        Toast.makeText(getActivity(), "Turn On Mobile Data or Wifi", Toast.LENGTH_SHORT).show();
    }

    return mView;
}

    private boolean networkAvailable() {

    ConnectivityManager cm = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = cm.getActiveNetworkInfo();
    boolean isAvailable = false;
    if (networkInfo != null && networkInfo.isConnected()){
        isAvailable = true;
    }
    return isAvailable;
}
}

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您将使用自己的SQL在find_by_sql中查找表中的记录。即使基础查询只返回单个记录,find_by_sql方法也将返回一个对象数组。例如,您可以运行此查询:

例如

  Recipe.joins("INNER JOIN item ON <your_conditions_to_get_recipe_related_to_item>")

  Recipe.find_by_sql("SELECT * FROM recipe
       INNER JOIN item ON <your_conditions_to_get_recipe_related_to_item>")

根据你的情况

例如

  Recipe.joins("INNER JOIN item ON item.recipe_id = recipe.id and item.id = 1 and recipe.id = 2")

与params

  Recipe.joins("INNER JOIN item ON item.recipe_id = recipe.id").where("item.id = ? and recipe.id = ?", item_id, recipe_id)

这些是访问数据库并检索模型或模型实例而无需关联方式的一些方法。